

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>dcase_framework.keras_utils &mdash; DCASE2017 Baseline 1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  

  
    <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
  

  
        <link rel="index" title="Index"
              href="../../genindex.html"/>
        <link rel="search" title="Search" href="../../search.html"/>
    <link rel="top" title="DCASE2017 Baseline 1.0 documentation" href="../../index.html"/>
        <link rel="up" title="Module code" href="../index.html"/> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> DCASE2017 Baseline
          

          
            
            <img src="../../_static/dcase2017_baseline.png" class="logo" />
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Baseline system</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../system_description.html">System description</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../system_description.html#mlp-based-system-dcase2017-baseline">MLP based system, DCASE2017 baseline</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../system_description.html#gmm-based-approach">GMM based approach</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../system_description.html#processing-blocks">Processing blocks</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../applications.html">Applications</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../applications.html#task1-acoustic-scene-classification"> Acoustic scene classification</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../applications.html#results">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../applications.html#task2-detection-of-rare-sound-events"> Detection of rare sound events</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../applications.html#id2">Results</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../applications.html#task3-sound-event-detection-in-real-life-audio"> Sound event detection in real life audio</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../applications.html#id3">Results</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../usage_tutorial.html">Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../usage_tutorial.html#application-arguments">Application arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../usage_tutorial.html#basic-usage">Basic usage</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../parameterization.html">Parameterization</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../parameterization.html#parameter-overwriting">Parameter overwriting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../parameterization.html#parameter-file">Parameter file</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../parameterization.html#parameter-hash">Parameter hash</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../parameterization.html#parameter-sections">Parameter sections</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#flow">Flow</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#general">General</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#path">Path</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#dataset">Dataset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#feature-extractor">Feature extractor</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#feature-stacker">Feature stacker</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#feature-normalizer">Feature normalizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#feature-aggregator">Feature aggregator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#learner">Learner</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#recognizer">Recognizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#evaluator">Evaluator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../parameterization.html#logging">Logging</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../reproducibility.html">Reproducibility</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../reproducibility.html#blas-libraries">BLAS libraries</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../reproducibility.html#intel-math-kernel">Intel Math Kernel</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../reproducibility.html#running-the-baseline-system">Running the baseline system</a></li>
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">DCASE Framework</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../framework.html">Introduction</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../framework.html#training-process">Training process</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../framework.html#testing-process">Testing process</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../extending_framework.html">Extending the framework</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../extending_framework.html#adding-datasets">Adding datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../extending_framework.html#adding-features">Adding features</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../extending_framework.html#addinng-learners">Addinng learners</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../extending_framework.html#extending-applicationcore">Extending ApplicationCore</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../application_core.html">Application core</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../application_core.html#acousticsceneclassificationappcore">AcousticSceneClassificationAppCore</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.html">dcase_framework.application_core.AcousticSceneClassificationAppCore</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.show_dataset_list.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.show_dataset_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.show_parameters.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.show_parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.initialize.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.initialize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.feature_extraction.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.feature_extraction</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.feature_normalization.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.feature_normalization</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.system_training.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.system_training</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.system_testing.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.system_testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AcousticSceneClassificationAppCore.system_evaluation.html">dcase_framework.application_core.AcousticSceneClassificationAppCore.system_evaluation</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../application_core.html#soundeventappcore">SoundEventAppCore</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.html">dcase_framework.application_core.SoundEventAppCore</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.show_dataset_list.html">dcase_framework.application_core.SoundEventAppCore.show_dataset_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.show_parameters.html">dcase_framework.application_core.SoundEventAppCore.show_parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.initialize.html">dcase_framework.application_core.SoundEventAppCore.initialize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.feature_extraction.html">dcase_framework.application_core.SoundEventAppCore.feature_extraction</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.feature_normalization.html">dcase_framework.application_core.SoundEventAppCore.feature_normalization</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.system_training.html">dcase_framework.application_core.SoundEventAppCore.system_training</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.system_testing.html">dcase_framework.application_core.SoundEventAppCore.system_testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.SoundEventAppCore.system_evaluation.html">dcase_framework.application_core.SoundEventAppCore.system_evaluation</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../application_core.html#binarysoundeventappcore">BinarySoundEventAppCore</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.html">dcase_framework.application_core.BinarySoundEventAppCore</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.show_dataset_list.html">dcase_framework.application_core.BinarySoundEventAppCore.show_dataset_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.show_parameters.html">dcase_framework.application_core.BinarySoundEventAppCore.show_parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.initialize.html">dcase_framework.application_core.BinarySoundEventAppCore.initialize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.feature_extraction.html">dcase_framework.application_core.BinarySoundEventAppCore.feature_extraction</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.feature_normalization.html">dcase_framework.application_core.BinarySoundEventAppCore.feature_normalization</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.system_training.html">dcase_framework.application_core.BinarySoundEventAppCore.system_training</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.system_testing.html">dcase_framework.application_core.BinarySoundEventAppCore.system_testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.BinarySoundEventAppCore.system_evaluation.html">dcase_framework.application_core.BinarySoundEventAppCore.system_evaluation</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../application_core.html#appcore-base-class">AppCore &#8211; base class</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.html">dcase_framework.application_core.AppCore</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.show_dataset_list.html">dcase_framework.application_core.AppCore.show_dataset_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.show_parameters.html">dcase_framework.application_core.AppCore.show_parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.initialize.html">dcase_framework.application_core.AppCore.initialize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.feature_extraction.html">dcase_framework.application_core.AppCore.feature_extraction</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.feature_normalization.html">dcase_framework.application_core.AppCore.feature_normalization</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.system_training.html">dcase_framework.application_core.AppCore.system_training</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.system_testing.html">dcase_framework.application_core.AppCore.system_testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.application_core.AppCore.system_evaluation.html">dcase_framework.application_core.AppCore.system_evaluation</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../parameters.html">Parameters</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../parameters.html#recipe">Recipe</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../parameters.html#paths-and-parameter-hash">Paths and parameter hash</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../parameters.html#parametercontainer">ParameterContainer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.html">dcase_framework.parameters.ParameterContainer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.load.html">dcase_framework.parameters.ParameterContainer.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.save.html">dcase_framework.parameters.ParameterContainer.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.exists.html">dcase_framework.parameters.ParameterContainer.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.get_path.html">dcase_framework.parameters.ParameterContainer.get_path</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.show.html">dcase_framework.parameters.ParameterContainer.show</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.log.html">dcase_framework.parameters.ParameterContainer.log</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.override.html">dcase_framework.parameters.ParameterContainer.override</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.process.html">dcase_framework.parameters.ParameterContainer.process</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.process_method_parameters.html">dcase_framework.parameters.ParameterContainer.process_method_parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.parameters.ParameterContainer.get_hash.html">dcase_framework.parameters.ParameterContainer.get_hash</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../files.html">Files</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#audiofile">AudioFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.AudioFile.html">dcase_framework.files.AudioFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.AudioFile.load.html">dcase_framework.files.AudioFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.AudioFile.save.html">dcase_framework.files.AudioFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.AudioFile.exists.html">dcase_framework.files.AudioFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.AudioFile.empty.html">dcase_framework.files.AudioFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#parameterfile">ParameterFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ParameterFile.html">dcase_framework.files.ParameterFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ParameterFile.load.html">dcase_framework.files.ParameterFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ParameterFile.save.html">dcase_framework.files.ParameterFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ParameterFile.exists.html">dcase_framework.files.ParameterFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ParameterFile.empty.html">dcase_framework.files.ParameterFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#featurefile">FeatureFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.FeatureFile.html">dcase_framework.files.FeatureFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.FeatureFile.load.html">dcase_framework.files.FeatureFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.FeatureFile.save.html">dcase_framework.files.FeatureFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.FeatureFile.exists.html">dcase_framework.files.FeatureFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.FeatureFile.empty.html">dcase_framework.files.FeatureFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#datafile">DataFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DataFile.html">dcase_framework.files.DataFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DataFile.load.html">dcase_framework.files.DataFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DataFile.save.html">dcase_framework.files.DataFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DataFile.exists.html">dcase_framework.files.DataFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DataFile.empty.html">dcase_framework.files.DataFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#repositoryfile">RepositoryFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.RepositoryFile.html">dcase_framework.files.RepositoryFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.RepositoryFile.load.html">dcase_framework.files.RepositoryFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.RepositoryFile.exists.html">dcase_framework.files.RepositoryFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.RepositoryFile.empty.html">dcase_framework.files.RepositoryFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#textfile">TextFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.TextFile.html">dcase_framework.files.TextFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.TextFile.load.html">dcase_framework.files.TextFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.TextFile.save.html">dcase_framework.files.TextFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.TextFile.exists.html">dcase_framework.files.TextFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.TextFile.empty.html">dcase_framework.files.TextFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#dictfile">DictFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DictFile.html">dcase_framework.files.DictFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DictFile.load.html">dcase_framework.files.DictFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DictFile.save.html">dcase_framework.files.DictFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DictFile.exists.html">dcase_framework.files.DictFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.DictFile.empty.html">dcase_framework.files.DictFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#listfile">ListFile</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ListFile.html">dcase_framework.files.ListFile</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ListFile.load.html">dcase_framework.files.ListFile.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ListFile.save.html">dcase_framework.files.ListFile.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ListFile.exists.html">dcase_framework.files.ListFile.exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.ListFile.empty.html">dcase_framework.files.ListFile.empty</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../files.html#mixins">Mixins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.files.FileMixin.html">dcase_framework.files.FileMixin</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../datasets.html">Datasets</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../datasets.html#dataset-base-class">Dataset - Base class</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.html">dcase_framework.datasets.Dataset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.initialize.html">dcase_framework.datasets.Dataset.initialize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.show_info.html">dcase_framework.datasets.Dataset.show_info</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.audio_files.html">dcase_framework.datasets.Dataset.audio_files</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.audio_file_count.html">dcase_framework.datasets.Dataset.audio_file_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.meta.html">dcase_framework.datasets.Dataset.meta</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.meta_count.html">dcase_framework.datasets.Dataset.meta_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.error_meta.html">dcase_framework.datasets.Dataset.error_meta</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.error_meta_count.html">dcase_framework.datasets.Dataset.error_meta_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.fold_count.html">dcase_framework.datasets.Dataset.fold_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.scene_labels.html">dcase_framework.datasets.Dataset.scene_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.scene_label_count.html">dcase_framework.datasets.Dataset.scene_label_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.event_labels.html">dcase_framework.datasets.Dataset.event_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.event_label_count.html">dcase_framework.datasets.Dataset.event_label_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.audio_tags.html">dcase_framework.datasets.Dataset.audio_tags</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.audio_tag_count.html">dcase_framework.datasets.Dataset.audio_tag_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.download_packages.html">dcase_framework.datasets.Dataset.download_packages</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.extract.html">dcase_framework.datasets.Dataset.extract</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.train.html">dcase_framework.datasets.Dataset.train</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.test.html">dcase_framework.datasets.Dataset.test</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.eval.html">dcase_framework.datasets.Dataset.eval</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.train_files.html">dcase_framework.datasets.Dataset.train_files</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.test_files.html">dcase_framework.datasets.Dataset.test_files</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.eval_files.html">dcase_framework.datasets.Dataset.eval_files</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.validation_files.html">dcase_framework.datasets.Dataset.validation_files</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.folds.html">dcase_framework.datasets.Dataset.folds</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.file_meta.html">dcase_framework.datasets.Dataset.file_meta</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.file_error_meta.html">dcase_framework.datasets.Dataset.file_error_meta</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.file_error_meta.html">dcase_framework.datasets.Dataset.file_error_meta</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.relative_to_absolute_path.html">dcase_framework.datasets.Dataset.relative_to_absolute_path</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.Dataset.absolute_to_relative.html">dcase_framework.datasets.Dataset.absolute_to_relative</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../datasets.html#acousticscenedataset">AcousticSceneDataset</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.AcousticSceneDataset.html">dcase_framework.datasets.AcousticSceneDataset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTAcousticScenes_2017_DevelopmentSet.html">dcase_framework.datasets.TUTAcousticScenes_2017_DevelopmentSet</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTAcousticScenes_2016_DevelopmentSet.html">dcase_framework.datasets.TUTAcousticScenes_2016_DevelopmentSet</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTAcousticScenes_2016_EvaluationSet.html">dcase_framework.datasets.TUTAcousticScenes_2016_EvaluationSet</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../datasets.html#soundeventdataset">SoundEventDataset</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.SoundEventDataset.html">dcase_framework.datasets.SoundEventDataset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.SoundEventDataset.event_label_count.html">dcase_framework.datasets.SoundEventDataset.event_label_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.SoundEventDataset.event_labels.html">dcase_framework.datasets.SoundEventDataset.event_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.SoundEventDataset.train.html">dcase_framework.datasets.SoundEventDataset.train</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.SoundEventDataset.test.html">dcase_framework.datasets.SoundEventDataset.test</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTRareSoundEvents_2017_DevelopmentSet.html">dcase_framework.datasets.TUTRareSoundEvents_2017_DevelopmentSet</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTSoundEvents_2017_DevelopmentSet.html">dcase_framework.datasets.TUTSoundEvents_2017_DevelopmentSet</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTSoundEvents_2016_DevelopmentSet.html">dcase_framework.datasets.TUTSoundEvents_2016_DevelopmentSet</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.TUTSoundEvents_2016_EvaluationSet.html">dcase_framework.datasets.TUTSoundEvents_2016_EvaluationSet</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../datasets.html#audiotaggingdataset">AudioTaggingDataset</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.datasets.AudioTaggingDataset.html">dcase_framework.datasets.AudioTaggingDataset</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../metadata.html">Meta data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../metadata.html#metadataitem">MetaDataItem</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.html">dcase_framework.metadata.MetaDataItem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.id.html">dcase_framework.metadata.MetaDataItem.id</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.file.html">dcase_framework.metadata.MetaDataItem.file</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.scene_label.html">dcase_framework.metadata.MetaDataItem.scene_label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.event_label.html">dcase_framework.metadata.MetaDataItem.event_label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.event_onset.html">dcase_framework.metadata.MetaDataItem.event_onset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.event_offset.html">dcase_framework.metadata.MetaDataItem.event_offset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.identifier.html">dcase_framework.metadata.MetaDataItem.identifier</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataItem.source_label.html">dcase_framework.metadata.MetaDataItem.source_label</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../metadata.html#metadatacontainer">MetaDataContainer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.html">dcase_framework.metadata.MetaDataContainer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.log.html">dcase_framework.metadata.MetaDataContainer.log</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.show.html">dcase_framework.metadata.MetaDataContainer.show</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.get_string.html">dcase_framework.metadata.MetaDataContainer.get_string</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.update.html">dcase_framework.metadata.MetaDataContainer.update</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.filter.html">dcase_framework.metadata.MetaDataContainer.filter</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.filter_time_segment.html">dcase_framework.metadata.MetaDataContainer.filter_time_segment</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.process_events.html">dcase_framework.metadata.MetaDataContainer.process_events</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.remove_field.html">dcase_framework.metadata.MetaDataContainer.remove_field</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.slice_field.html">dcase_framework.metadata.MetaDataContainer.slice_field</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.filter_time_segment.html">dcase_framework.metadata.MetaDataContainer.filter_time_segment</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.add_time_offset.html">dcase_framework.metadata.MetaDataContainer.add_time_offset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.file_list.html">dcase_framework.metadata.MetaDataContainer.file_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.event_count.html">dcase_framework.metadata.MetaDataContainer.event_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.scene_label_count.html">dcase_framework.metadata.MetaDataContainer.scene_label_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.event_label_count.html">dcase_framework.metadata.MetaDataContainer.event_label_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.unique_scene_labels.html">dcase_framework.metadata.MetaDataContainer.unique_scene_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.unique_event_labels.html">dcase_framework.metadata.MetaDataContainer.unique_event_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.max_offset.html">dcase_framework.metadata.MetaDataContainer.max_offset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.load.html">dcase_framework.metadata.MetaDataContainer.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.save.html">dcase_framework.metadata.MetaDataContainer.save</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.event_stat_counts.html">dcase_framework.metadata.MetaDataContainer.event_stat_counts</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.MetaDataContainer.event_roll.html">dcase_framework.metadata.MetaDataContainer.event_roll</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../metadata.html#eventroll">EventRoll</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.EventRoll.html">dcase_framework.metadata.EventRoll</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.EventRoll.roll.html">dcase_framework.metadata.EventRoll.roll</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.EventRoll.pad.html">dcase_framework.metadata.EventRoll.pad</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.EventRoll.plot.html">dcase_framework.metadata.EventRoll.plot</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../metadata.html#probabilityitem">ProbabilityItem</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.html">dcase_framework.metadata.ProbabilityItem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.id.html">dcase_framework.metadata.ProbabilityItem.id</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.file.html">dcase_framework.metadata.ProbabilityItem.file</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.label.html">dcase_framework.metadata.ProbabilityItem.label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.timestamp.html">dcase_framework.metadata.ProbabilityItem.timestamp</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.probability.html">dcase_framework.metadata.ProbabilityItem.probability</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityItem.get_list.html">dcase_framework.metadata.ProbabilityItem.get_list</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../metadata.html#probabilitycontainer">ProbabilityContainer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.html">dcase_framework.metadata.ProbabilityContainer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.log.html">dcase_framework.metadata.ProbabilityContainer.log</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.show.html">dcase_framework.metadata.ProbabilityContainer.show</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.update.html">dcase_framework.metadata.ProbabilityContainer.update</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.file_list.html">dcase_framework.metadata.ProbabilityContainer.file_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.unique_labels.html">dcase_framework.metadata.ProbabilityContainer.unique_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.filter.html">dcase_framework.metadata.ProbabilityContainer.filter</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.get_string.html">dcase_framework.metadata.ProbabilityContainer.get_string</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.load.html">dcase_framework.metadata.ProbabilityContainer.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.metadata.ProbabilityContainer.save.html">dcase_framework.metadata.ProbabilityContainer.save</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../features.html">Features</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featurecontainer">FeatureContainer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.html">dcase_framework.features.FeatureContainer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.show.html">dcase_framework.features.FeatureContainer.show</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.log.html">dcase_framework.features.FeatureContainer.log</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.get_path.html">dcase_framework.features.FeatureContainer.get_path</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.shape.html">dcase_framework.features.FeatureContainer.shape</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.channels.html">dcase_framework.features.FeatureContainer.channels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.frames.html">dcase_framework.features.FeatureContainer.frames</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.vector_length.html">dcase_framework.features.FeatureContainer.vector_length</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.feat.html">dcase_framework.features.FeatureContainer.feat</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.stat.html">dcase_framework.features.FeatureContainer.stat</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.meta.html">dcase_framework.features.FeatureContainer.meta</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureContainer.load.html">dcase_framework.features.FeatureContainer.load</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featurerepository">FeatureRepository</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureRepository.html">dcase_framework.features.FeatureRepository</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureRepository.show.html">dcase_framework.features.FeatureRepository.show</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureRepository.log.html">dcase_framework.features.FeatureRepository.log</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureRepository.get_path.html">dcase_framework.features.FeatureRepository.get_path</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureRepository.load.html">dcase_framework.features.FeatureRepository.load</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featureextractor">FeatureExtractor</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureExtractor.html">dcase_framework.features.FeatureExtractor</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureExtractor.extract.html">dcase_framework.features.FeatureExtractor.extract</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureExtractor.get_default_parameters.html">dcase_framework.features.FeatureExtractor.get_default_parameters</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featurenormalizer">FeatureNormalizer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureNormalizer.html">dcase_framework.features.FeatureNormalizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureNormalizer.accumulate.html">dcase_framework.features.FeatureNormalizer.accumulate</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureNormalizer.finalize.html">dcase_framework.features.FeatureNormalizer.finalize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureNormalizer.normalize.html">dcase_framework.features.FeatureNormalizer.normalize</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureNormalizer.process.html">dcase_framework.features.FeatureNormalizer.process</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featurestacker">FeatureStacker</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureStacker.html">dcase_framework.features.FeatureStacker</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureStacker.normalizer.html">dcase_framework.features.FeatureStacker.normalizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureStacker.feature_vector.html">dcase_framework.features.FeatureStacker.feature_vector</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureStacker.process.html">dcase_framework.features.FeatureStacker.process</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featureaggregator">FeatureAggregator</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureAggregator.html">dcase_framework.features.FeatureAggregator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureAggregator.process.html">dcase_framework.features.FeatureAggregator.process</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../features.html#featuremasker">FeatureMasker</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureMasker.html">dcase_framework.features.FeatureMasker</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.features.FeatureMasker.process.html">dcase_framework.features.FeatureMasker.process</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../learners.html">Learners</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../learners.html#sceneclassifier">SceneClassifier</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../learners.html#sceneclassifiergmm">SceneClassifierGMM</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierGMM.html">dcase_framework.learners.SceneClassifierGMM</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierGMM.learn.html">dcase_framework.learners.SceneClassifierGMM.learn</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierGMM.predict.html">dcase_framework.learners.SceneClassifierGMM.predict</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../learners.html#sceneclassifiermlp">SceneClassifierMLP</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierMLP.html">dcase_framework.learners.SceneClassifierMLP</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierMLP.learn.html">dcase_framework.learners.SceneClassifierMLP.learn</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierMLP.predict.html">dcase_framework.learners.SceneClassifierMLP.predict</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../learners.html#sceneclassifierkerassequential">SceneClassifierKerasSequential</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierKerasSequential.html">dcase_framework.learners.SceneClassifierKerasSequential</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierKerasSequential.learn.html">dcase_framework.learners.SceneClassifierKerasSequential.learn</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.SceneClassifierKerasSequential.predict.html">dcase_framework.learners.SceneClassifierKerasSequential.predict</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../learners.html#eventdetector">EventDetector</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetector.html">dcase_framework.learners.EventDetector</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../learners.html#eventdetectorgmm">EventDetectorGMM</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorGMM.html">dcase_framework.learners.EventDetectorGMM</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorGMM.learn.html">dcase_framework.learners.EventDetectorGMM.learn</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorGMM.predict.html">dcase_framework.learners.EventDetectorGMM.predict</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../learners.html#eventdetectormlp">EventDetectorMLP</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorMLP.html">dcase_framework.learners.EventDetectorMLP</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorMLP.learn.html">dcase_framework.learners.EventDetectorMLP.learn</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorMLP.predict.html">dcase_framework.learners.EventDetectorMLP.predict</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../learners.html#eventdetectorkerassequential">EventDetectorKerasSequential</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorKerasSequential.html">dcase_framework.learners.EventDetectorKerasSequential</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorKerasSequential.learn.html">dcase_framework.learners.EventDetectorKerasSequential.learn</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../generated/dcase_framework.learners.EventDetectorKerasSequential.predict.html">dcase_framework.learners.EventDetectorKerasSequential.predict</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../learners.html#learnercontainer-base-class">LearnerContainer - Base class</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.html">dcase_framework.learners.LearnerContainer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.class_labels.html">dcase_framework.learners.LearnerContainer.class_labels</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.method.html">dcase_framework.learners.LearnerContainer.method</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.params.html">dcase_framework.learners.LearnerContainer.params</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.feature_masker.html">dcase_framework.learners.LearnerContainer.feature_masker</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.feature_normalizer.html">dcase_framework.learners.LearnerContainer.feature_normalizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.feature_stacker.html">dcase_framework.learners.LearnerContainer.feature_stacker</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.feature_aggregator.html">dcase_framework.learners.LearnerContainer.feature_aggregator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.model.html">dcase_framework.learners.LearnerContainer.model</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.set_seed.html">dcase_framework.learners.LearnerContainer.set_seed</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.learners.LearnerContainer.learner_params.html">dcase_framework.learners.LearnerContainer.learner_params</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../recognizers.html">Recognizers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../recognizers.html#scenerecognizer">SceneRecognizer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.SceneRecognizer.html">dcase_framework.recognizers.SceneRecognizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.SceneRecognizer.process.html">dcase_framework.recognizers.SceneRecognizer.process</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../recognizers.html#eventrecognizer">EventRecognizer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.EventRecognizer.html">dcase_framework.recognizers.EventRecognizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.EventRecognizer.process.html">dcase_framework.recognizers.EventRecognizer.process</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.EventRecognizer.process_ratio.html">dcase_framework.recognizers.EventRecognizer.process_ratio</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../recognizers.html#baserecognizer">BaseRecognizer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.BaseRecognizer.html">dcase_framework.recognizers.BaseRecognizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.BaseRecognizer.collapse_probabilities.html">dcase_framework.recognizers.BaseRecognizer.collapse_probabilities</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.BaseRecognizer.collapse_probabilities_windowed.html">dcase_framework.recognizers.BaseRecognizer.collapse_probabilities_windowed</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.BaseRecognizer.find_contiguous_regions.html">dcase_framework.recognizers.BaseRecognizer.find_contiguous_regions</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.recognizers.BaseRecognizer.process_activity.html">dcase_framework.recognizers.BaseRecognizer.process_activity</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../data.html">Data utils</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../data.html#datasequencer">DataSequencer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataSequencer.html">dcase_framework.data.DataSequencer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataSequencer.process.html">dcase_framework.data.DataSequencer.process</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataSequencer.increase_shifting.html">dcase_framework.data.DataSequencer.increase_shifting</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../data.html#dataprocessor">DataProcessor</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.html">dcase_framework.data.DataProcessor</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.load.html">dcase_framework.data.DataProcessor.load</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.process.html">dcase_framework.data.DataProcessor.process</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.process_features.html">dcase_framework.data.DataProcessor.process_features</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.process_activity_data.html">dcase_framework.data.DataProcessor.process_activity_data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.process_data.html">dcase_framework.data.DataProcessor.process_data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataProcessor.call_method.html">dcase_framework.data.DataProcessor.call_method</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../data.html#databuffer">DataBuffer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.html">dcase_framework.data.DataBuffer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.count.html">dcase_framework.data.DataBuffer.count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.full.html">dcase_framework.data.DataBuffer.full</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.key_exists.html">dcase_framework.data.DataBuffer.key_exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.set.html">dcase_framework.data.DataBuffer.set</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.get.html">dcase_framework.data.DataBuffer.get</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.DataBuffer.clear.html">dcase_framework.data.DataBuffer.clear</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../data.html#processingchain">ProcessingChain</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.ProcessingChain.html">dcase_framework.data.ProcessingChain</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.ProcessingChain.process.html">dcase_framework.data.ProcessingChain.process</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.data.ProcessingChain.call_method.html">dcase_framework.data.ProcessingChain.call_method</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../keras_utils.html">Keras utils</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#kerasmixin">KerasMixin</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.create_model.html">dcase_framework.keras_utils.KerasMixin.create_model</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.create_callback_list.html">dcase_framework.keras_utils.KerasMixin.create_callback_list</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.create_external_metric_evaluators.html">dcase_framework.keras_utils.KerasMixin.create_external_metric_evaluators</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.prepare_data.html">dcase_framework.keras_utils.KerasMixin.prepare_data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.prepare_activity.html">dcase_framework.keras_utils.KerasMixin.prepare_activity</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.keras_model_exists.html">dcase_framework.keras_utils.KerasMixin.keras_model_exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.log_model_summary.html">dcase_framework.keras_utils.KerasMixin.log_model_summary</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.plot_model.html">dcase_framework.keras_utils.KerasMixin.plot_model</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.KerasMixin.get_processing_interval.html">dcase_framework.keras_utils.KerasMixin.get_processing_interval</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#basecallback">BaseCallback</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.BaseCallback.html">dcase_framework.keras_utils.BaseCallback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#progressloggercallback">ProgressLoggerCallback</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.ProgressLoggerCallback.html">dcase_framework.keras_utils.ProgressLoggerCallback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#progressplottercallback">ProgressPlotterCallback</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.ProgressPlotterCallback.html">dcase_framework.keras_utils.ProgressPlotterCallback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#stoppercallback">StopperCallback</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.StopperCallback.html">dcase_framework.keras_utils.StopperCallback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#stashercallback">StasherCallback</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.StasherCallback.html">dcase_framework.keras_utils.StasherCallback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#basedatagenerator">BaseDataGenerator</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.html">dcase_framework.keras_utils.BaseDataGenerator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.input_size.html">dcase_framework.keras_utils.BaseDataGenerator.input_size</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.data_size.html">dcase_framework.keras_utils.BaseDataGenerator.data_size</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.steps_count.html">dcase_framework.keras_utils.BaseDataGenerator.steps_count</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.info.html">dcase_framework.keras_utils.BaseDataGenerator.info</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../keras_utils.html#featuregenerator">FeatureGenerator</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.FeatureGenerator.html">dcase_framework.keras_utils.FeatureGenerator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.keras_utils.FeatureGenerator.generator.html">dcase_framework.keras_utils.FeatureGenerator.generator</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../ui.html">User interfacing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../ui.html#fancylogger">FancyLogger</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.html">dcase_framework.ui.FancyLogger</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.title.html">dcase_framework.ui.FancyLogger.title</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.section_header.html">dcase_framework.ui.FancyLogger.section_header</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.foot.html">dcase_framework.ui.FancyLogger.foot</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.line.html">dcase_framework.ui.FancyLogger.line</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.data.html">dcase_framework.ui.FancyLogger.data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.info.html">dcase_framework.ui.FancyLogger.info</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.debug.html">dcase_framework.ui.FancyLogger.debug</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.ui.FancyLogger.error.html">dcase_framework.ui.FancyLogger.error</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../utils.html">Utils</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../utils.html#utility-functions">Utility functions</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.setup_logging.html">dcase_framework.utils.setup_logging</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.get_parameter_hash.html">dcase_framework.utils.get_parameter_hash</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.get_class_inheritors.html">dcase_framework.utils.get_class_inheritors</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.get_byte_string.html">dcase_framework.utils.get_byte_string</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.argument_file_exists.html">dcase_framework.utils.argument_file_exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.filelist_exists.html">dcase_framework.utils.filelist_exists</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.posix_path.html">dcase_framework.utils.posix_path</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../utils.html#timer">Timer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.Timer.html">dcase_framework.utils.Timer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.Timer.start.html">dcase_framework.utils.Timer.start</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.Timer.stop.html">dcase_framework.utils.Timer.stop</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.Timer.elapsed.html">dcase_framework.utils.Timer.elapsed</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.Timer.get_string.html">dcase_framework.utils.Timer.get_string</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../utils.html#suppressstdoutandstderr">SuppressStdoutAndStderr</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.SuppressStdoutAndStderr.html">dcase_framework.utils.SuppressStdoutAndStderr</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../utils.html#simplemathstringevaluator">SimpleMathStringEvaluator</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.SimpleMathStringEvaluator.html">dcase_framework.utils.SimpleMathStringEvaluator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../generated/dcase_framework.utils.SimpleMathStringEvaluator.eval.html">dcase_framework.utils.SimpleMathStringEvaluator.eval</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">DCASE2017 Baseline</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>dcase_framework.keras_utils</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for dcase_framework.keras_utils</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Keras utils</span>
<span class="sd">===========</span>

<span class="sd">Utility classes related to Keras.</span>

<span class="sd">KerasMixin</span>
<span class="sd">^^^^^^^^^^</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    KerasMixin.create_model</span>
<span class="sd">    KerasMixin.create_callback_list</span>
<span class="sd">    KerasMixin.create_external_metric_evaluators</span>
<span class="sd">    KerasMixin.prepare_data</span>
<span class="sd">    KerasMixin.prepare_activity</span>
<span class="sd">    KerasMixin.keras_model_exists</span>
<span class="sd">    KerasMixin.log_model_summary</span>
<span class="sd">    KerasMixin.plot_model</span>
<span class="sd">    KerasMixin.get_processing_interval</span>

<span class="sd">BaseCallback</span>
<span class="sd">^^^^^^^^^^^^</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    BaseCallback</span>

<span class="sd">ProgressLoggerCallback</span>
<span class="sd">^^^^^^^^^^^^^^^^^^^^^^</span>

<span class="sd">Keras callback to store metrics with tqdm progress bar or logging interface. Implements Keras Callback API.</span>

<span class="sd">This callback is very similar to standard ``ProgbarLogger`` Keras callback, however it adds support for</span>
<span class="sd">logging interface and tqdm based progress bars, and external metrics</span>
<span class="sd">(metrics calculated outside Keras training process).</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    ProgressLoggerCallback</span>

<span class="sd">ProgressPlotterCallback</span>
<span class="sd">^^^^^^^^^^^^^^^^^^^^^^^</span>

<span class="sd">Keras callback to plot progress during the training process and save final progress into figure.</span>
<span class="sd">Implements Keras Callback API.</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    ProgressPlotterCallback</span>

<span class="sd">StopperCallback</span>
<span class="sd">^^^^^^^^^^^^^^^</span>

<span class="sd">Keras callback to stop training when improvement has not seen in specified amount of epochs.</span>
<span class="sd">Implements Keras Callback API.</span>

<span class="sd">This Callback is very similar to standard ``EarlyStopping`` Keras callback, however it adds support for</span>
<span class="sd">external metrics (metrics calculated outside Keras training process).</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    StopperCallback</span>

<span class="sd">StasherCallback</span>
<span class="sd">^^^^^^^^^^^^^^^</span>

<span class="sd">Keras callback to monitor training process and store best model. Implements Keras Callback API.</span>

<span class="sd">This callback is very similar to standard ``ModelCheckpoint`` Keras callback, however it adds support for</span>
<span class="sd">external metrics (metrics calculated outside Keras training process).</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    StasherCallback</span>

<span class="sd">BaseDataGenerator</span>
<span class="sd">^^^^^^^^^^^^^^^^^</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    BaseDataGenerator</span>
<span class="sd">    BaseDataGenerator.input_size</span>
<span class="sd">    BaseDataGenerator.data_size</span>
<span class="sd">    BaseDataGenerator.steps_count</span>
<span class="sd">    BaseDataGenerator.info</span>

<span class="sd">FeatureGenerator</span>
<span class="sd">^^^^^^^^^^^^^^^^</span>

<span class="sd">.. autosummary::</span>
<span class="sd">    :toctree: generated/</span>

<span class="sd">    FeatureGenerator</span>
<span class="sd">    FeatureGenerator.generator</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">importlib</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="k">import</span> <span class="n">tqdm</span>
<span class="kn">from</span> <span class="nn">six</span> <span class="k">import</span> <span class="n">iteritems</span>

<span class="kn">from</span> <span class="nn">.containers</span> <span class="k">import</span> <span class="n">DottedDict</span>
<span class="kn">from</span> <span class="nn">.utils</span> <span class="k">import</span> <span class="n">SuppressStdoutAndStderr</span><span class="p">,</span> <span class="n">Timer</span><span class="p">,</span> <span class="n">SimpleMathStringEvaluator</span><span class="p">,</span> <span class="n">get_parameter_hash</span>
<span class="kn">from</span> <span class="nn">.features</span> <span class="k">import</span> <span class="n">FeatureContainer</span>
<span class="kn">from</span> <span class="nn">.metadata</span> <span class="k">import</span> <span class="n">EventRoll</span>
<span class="kn">from</span> <span class="nn">.data</span> <span class="k">import</span> <span class="n">DataBuffer</span>


<span class="k">class</span> <span class="nc">KerasMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Class Mixin for Keras based learner containers.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">excluded_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">item</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">excluded_fields</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">item</span><span class="p">):</span>
                <span class="n">data</span><span class="p">[</span><span class="n">item</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">item</span><span class="p">))</span>
        <span class="n">data</span><span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;.model.hdf5&#39;</span>
        <span class="k">return</span> <span class="n">data</span>

<div class="viewcode-block" id="KerasMixin.keras_model_exists"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.keras_model_exists.html#dcase_framework.keras_utils.KerasMixin.keras_model_exists">[docs]</a>    <span class="k">def</span> <span class="nf">keras_model_exists</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check that keras model exists on disk</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        bool</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">keras_model_filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;.model.hdf5&#39;</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">keras_model_filename</span><span class="p">)</span></div>

<div class="viewcode-block" id="KerasMixin.log_model_summary"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.log_model_summary.html#dcase_framework.keras_utils.KerasMixin.log_model_summary">[docs]</a>    <span class="k">def</span> <span class="nf">log_model_summary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prints model summary to the logging interface.</span>

<span class="sd">        Similar to Keras model summary</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">layer_name_map</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;BatchNormalization&#39;</span><span class="p">:</span> <span class="s1">&#39;BatchNorm&#39;</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="kn">import</span> <span class="nn">keras</span>
        <span class="kn">from</span> <span class="nn">distutils.version</span> <span class="k">import</span> <span class="n">LooseVersion</span>
        <span class="kn">import</span> <span class="nn">keras.backend</span> <span class="k">as</span> <span class="nn">keras_backend</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  Model summary&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
            <span class="s1">&#39;    </span><span class="si">{type:&lt;15s}</span><span class="s1"> | </span><span class="si">{out:20s}</span><span class="s1"> | </span><span class="si">{param:6s}</span><span class="s1">  | </span><span class="si">{name:21s}</span><span class="s1">  | </span><span class="si">{conn:27s}</span><span class="s1"> | </span><span class="si">{act:7s}</span><span class="s1"> | </span><span class="si">{init:7s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;Layer type&#39;</span><span class="p">,</span>
                <span class="n">out</span><span class="o">=</span><span class="s1">&#39;Output&#39;</span><span class="p">,</span>
                <span class="n">param</span><span class="o">=</span><span class="s1">&#39;Param&#39;</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="s1">&#39;Name&#39;</span><span class="p">,</span>
                <span class="n">conn</span><span class="o">=</span><span class="s1">&#39;Connected to&#39;</span><span class="p">,</span>
                <span class="n">act</span><span class="o">=</span><span class="s1">&#39;Activ.&#39;</span><span class="p">,</span>
                <span class="n">init</span><span class="o">=</span><span class="s1">&#39;Init&#39;</span><span class="p">)</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
            <span class="s1">&#39;    </span><span class="si">{type:&lt;15s}</span><span class="s1"> + </span><span class="si">{out:20s}</span><span class="s1"> + </span><span class="si">{param:6s}</span><span class="s1">  + </span><span class="si">{name:21s}</span><span class="s1">  + </span><span class="si">{conn:27s}</span><span class="s1"> + </span><span class="si">{act:7s}</span><span class="s1"> + </span><span class="si">{init:6s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">15</span><span class="p">,</span>
                <span class="n">out</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">20</span><span class="p">,</span>
                <span class="n">param</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">6</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">21</span><span class="p">,</span>
                <span class="n">conn</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">27</span><span class="p">,</span>
                <span class="n">act</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">7</span><span class="p">,</span>
                <span class="n">init</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">6</span><span class="p">)</span>
        <span class="p">)</span>

        <span class="k">for</span> <span class="n">layer</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">layers</span><span class="p">:</span>
            <span class="n">connections</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">if</span> <span class="n">LooseVersion</span><span class="p">(</span><span class="n">keras</span><span class="o">.</span><span class="n">__version__</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="n">LooseVersion</span><span class="p">(</span><span class="s1">&#39;2.1.3&#39;</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">node_index</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">_inbound_nodes</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">inbound_layers</span><span class="p">)):</span>
                        <span class="n">inbound_layer</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">inbound_layers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
                        <span class="n">inbound_node_index</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">node_indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                        <span class="n">inbound_tensor_index</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">tensor_indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                        <span class="n">connections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inbound_layer</span> <span class="o">+</span> <span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">inbound_node_index</span><span class="p">)</span> <span class="o">+</span>
                                           <span class="s1">&#39;][&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">inbound_tensor_index</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">node_index</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">inbound_nodes</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">inbound_layers</span><span class="p">)):</span>
                        <span class="n">inbound_layer</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">inbound_layers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
                        <span class="n">inbound_node_index</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">node_indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                        <span class="n">inbound_tensor_index</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">tensor_indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                        <span class="n">connections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inbound_layer</span> <span class="o">+</span> <span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">inbound_node_index</span><span class="p">)</span> <span class="o">+</span>
                                           <span class="s1">&#39;][&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">inbound_tensor_index</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">)</span>
            <span class="n">config</span> <span class="o">=</span> <span class="n">DottedDict</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">get_config</span><span class="p">())</span>
            <span class="n">layer_name</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="k">if</span> <span class="n">layer_name</span> <span class="ow">in</span> <span class="n">layer_name_map</span><span class="p">:</span>
                <span class="n">layer_name</span> <span class="o">=</span> <span class="n">layer_name_map</span><span class="p">[</span><span class="n">layer_name</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;kernel_initializer.class_name&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;VarianceScaling&#39;</span><span class="p">:</span>
                <span class="n">init</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;kernel_initializer.config.distribution&#39;</span><span class="p">,</span> <span class="s1">&#39;---&#39;</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">config</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;kernel_initializer.class_name&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;RandomUniform&#39;</span><span class="p">:</span>
                <span class="n">init</span> <span class="o">=</span> <span class="s1">&#39;uniform&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">init</span> <span class="o">=</span> <span class="s1">&#39;---&#39;</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="s1">&#39;    </span><span class="si">{type:&lt;15s}</span><span class="s1"> | </span><span class="si">{shape:20s}</span><span class="s1"> | </span><span class="si">{params:6s}</span><span class="s1">  | </span><span class="si">{name:21s}</span><span class="s1">  | </span><span class="si">{connected:27s}</span><span class="s1"> | </span><span class="si">{activation:7s}</span><span class="s1"> | </span><span class="si">{init:7s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="nb">type</span><span class="o">=</span><span class="n">layer_name</span><span class="p">,</span>
                    <span class="n">shape</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">output_shape</span><span class="p">),</span>
                    <span class="n">params</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">count_params</span><span class="p">()),</span>
                    <span class="n">name</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">name</span><span class="p">),</span>
                    <span class="n">connected</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">connections</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">connections</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="s1">&#39;---&#39;</span><span class="p">,</span>
                    <span class="n">activation</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;activation&#39;</span><span class="p">,</span> <span class="s1">&#39;---&#39;</span><span class="p">)),</span>
                    <span class="n">init</span><span class="o">=</span><span class="n">init</span><span class="p">,</span>

                <span class="p">)</span>
            <span class="p">)</span>

        <span class="n">trainable_count</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="n">keras_backend</span><span class="o">.</span><span class="n">count_params</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">trainable_weights</span><span class="p">)])</span>
        <span class="p">)</span>

        <span class="n">non_trainable_count</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="n">keras_backend</span><span class="o">.</span><span class="n">count_params</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">non_trainable_weights</span><span class="p">)])</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  &#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  Parameters&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    Trainable</span><span class="se">\t</span><span class="s1">[</span><span class="si">{param_count:,}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">param_count</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">trainable_count</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    Non-Trainable</span><span class="se">\t</span><span class="s1">[</span><span class="si">{param_count:,}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">param_count</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">non_trainable_count</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
            <span class="s1">&#39;    Total</span><span class="se">\t\t</span><span class="s1">[</span><span class="si">{param_count:,}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">param_count</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">trainable_count</span> <span class="o">+</span> <span class="n">non_trainable_count</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  &#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="KerasMixin.plot_model"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.plot_model.html#dcase_framework.keras_utils.KerasMixin.plot_model">[docs]</a>    <span class="k">def</span> <span class="nf">plot_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;model.png&#39;</span><span class="p">,</span> <span class="n">show_shapes</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">show_layer_names</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Plots model topology</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="kn">from</span> <span class="nn">keras.utils.visualize_util</span> <span class="k">import</span> <span class="n">plot</span>
        <span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">to_file</span><span class="o">=</span><span class="n">filename</span><span class="p">,</span> <span class="n">show_shapes</span><span class="o">=</span><span class="n">show_shapes</span><span class="p">,</span> <span class="n">show_layer_names</span><span class="o">=</span><span class="n">show_layer_names</span><span class="p">)</span></div>

<div class="viewcode-block" id="KerasMixin.prepare_data"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.prepare_data.html#dcase_framework.keras_utils.KerasMixin.prepare_data">[docs]</a>    <span class="k">def</span> <span class="nf">prepare_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">processor</span><span class="o">=</span><span class="s1">&#39;default&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Concatenate feature data into one feature matrix</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data : dict of FeatureContainers</span>
<span class="sd">            Feature data</span>
<span class="sd">        files : list of str</span>
<span class="sd">            List of filenames</span>
<span class="sd">        processor : str (&#39;default&#39;, &#39;training&#39;)</span>
<span class="sd">            Data processor selector</span>
<span class="sd">            Default value &#39;default&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        numpy.ndarray</span>
<span class="sd">            Features concatenated</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;input_sequencer.enable&#39;</span><span class="p">):</span>
            <span class="n">processed_data</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">processor</span> <span class="o">==</span> <span class="s1">&#39;training&#39;</span><span class="p">:</span>
                    <span class="n">processed_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data_processor_training</span><span class="o">.</span><span class="n">process_data</span><span class="p">(</span>
                            <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="n">item</span><span class="p">]</span><span class="o">.</span><span class="n">feat</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

                <span class="k">else</span><span class="p">:</span>
                    <span class="n">processed_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data_processor</span><span class="o">.</span><span class="n">process_data</span><span class="p">(</span>
                            <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="n">item</span><span class="p">]</span><span class="o">.</span><span class="n">feat</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">processed_data</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">data</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">.</span><span class="n">feat</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">files</span><span class="p">])</span></div>

<div class="viewcode-block" id="KerasMixin.prepare_activity"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.prepare_activity.html#dcase_framework.keras_utils.KerasMixin.prepare_activity">[docs]</a>    <span class="k">def</span> <span class="nf">prepare_activity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">activity_matrix_dict</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">processor</span><span class="o">=</span><span class="s1">&#39;default&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Concatenate activity matrices into one activity matrix</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        activity_matrix_dict : dict of binary matrices</span>
<span class="sd">            Meta data</span>
<span class="sd">        files : list of str</span>
<span class="sd">            List of filenames</span>
<span class="sd">        processor : str (&#39;default&#39;, &#39;training&#39;)</span>
<span class="sd">            Data processor selector</span>
<span class="sd">            Default value &#39;default&#39;</span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        numpy.ndarray</span>
<span class="sd">            Activity matrix</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;input_sequencer.enable&#39;</span><span class="p">):</span>
            <span class="n">processed_activity</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">processor</span> <span class="o">==</span> <span class="s1">&#39;training&#39;</span><span class="p">:</span>
                    <span class="n">processed_activity</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data_processor_training</span><span class="o">.</span><span class="n">process_activity_data</span><span class="p">(</span>
                            <span class="n">activity_data</span><span class="o">=</span><span class="n">activity_matrix_dict</span><span class="p">[</span><span class="n">item</span><span class="p">]</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

                <span class="k">else</span><span class="p">:</span>
                    <span class="n">processed_activity</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data_processor</span><span class="o">.</span><span class="n">process_activity_data</span><span class="p">(</span>
                            <span class="n">activity_data</span><span class="o">=</span><span class="n">activity_matrix_dict</span><span class="p">[</span><span class="n">item</span><span class="p">]</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">processed_activity</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">activity_matrix_dict</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">files</span><span class="p">])</span></div>

<div class="viewcode-block" id="KerasMixin.create_model"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.create_model.html#dcase_framework.keras_utils.KerasMixin.create_model">[docs]</a>    <span class="k">def</span> <span class="nf">create_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create sequential Keras model</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="kn">from</span> <span class="nn">keras.models</span> <span class="k">import</span> <span class="n">Sequential</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">Sequential</span><span class="p">()</span>

        <span class="n">tuple_fields</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s1">&#39;input_shape&#39;</span><span class="p">,</span>
            <span class="s1">&#39;kernel_size&#39;</span><span class="p">,</span>
            <span class="s1">&#39;pool_size&#39;</span><span class="p">,</span>
            <span class="s1">&#39;dims&#39;</span><span class="p">,</span>
            <span class="s1">&#39;target_shape&#39;</span>
        <span class="p">]</span>

        <span class="c1"># Get model config parameters</span>
        <span class="n">model_params</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.config&#39;</span><span class="p">))</span>

        <span class="c1"># Get constants for model</span>
        <span class="n">constants</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.constants&#39;</span><span class="p">,</span> <span class="p">{}))</span>
        <span class="n">constants</span><span class="p">[</span><span class="s1">&#39;CLASS_COUNT&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="p">)</span>
        <span class="n">constants</span><span class="p">[</span><span class="s1">&#39;FEATURE_VECTOR_LENGTH&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">input_shape</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;input_sequencer.frames&#39;</span><span class="p">):</span>
            <span class="n">constants</span><span class="p">[</span><span class="s1">&#39;INPUT_SEQUENCE_LENGTH&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;input_sequencer.frames&#39;</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">process_field</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">constants_dict</span><span class="p">):</span>
            <span class="n">math_eval</span> <span class="o">=</span> <span class="n">SimpleMathStringEvaluator</span><span class="p">()</span>

            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="c1"># String field</span>
                <span class="k">if</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">constants_dict</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">constants_dict</span><span class="p">[</span><span class="n">value</span><span class="p">]</span>
                <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">sub_fields</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">subfield_id</span><span class="p">,</span> <span class="n">subfield</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">sub_fields</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">subfield</span> <span class="ow">in</span> <span class="n">constants_dict</span><span class="p">:</span>
                            <span class="n">sub_fields</span><span class="p">[</span><span class="n">subfield_id</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">constants_dict</span><span class="p">[</span><span class="n">subfield</span><span class="p">])</span>
                    <span class="k">return</span> <span class="n">math_eval</span><span class="o">.</span><span class="n">eval</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sub_fields</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">value</span>

            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="n">processed_value_list</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">item_id</span><span class="p">,</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
                    <span class="n">processed_value_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">process_field</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">item</span><span class="p">,</span> <span class="n">constants_dict</span><span class="o">=</span><span class="n">constants_dict</span><span class="p">))</span>
                <span class="k">return</span> <span class="n">processed_value_list</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">value</span>

        <span class="c1"># Inject constant into constants with equations</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">constants</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">constants</span><span class="p">[</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="n">process_field</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">constants</span><span class="p">[</span><span class="n">field</span><span class="p">],</span> <span class="n">constants_dict</span><span class="o">=</span><span class="n">constants</span><span class="p">)</span>

        <span class="c1"># Setup layers</span>
        <span class="k">for</span> <span class="n">layer_id</span><span class="p">,</span> <span class="n">layer_setup</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">model_params</span><span class="p">):</span>
            <span class="c1"># Get layer parameters</span>
            <span class="n">layer_setup</span> <span class="o">=</span> <span class="n">DottedDict</span><span class="p">(</span><span class="n">layer_setup</span><span class="p">)</span>
            <span class="k">if</span> <span class="s1">&#39;config&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">layer_setup</span><span class="p">:</span>
                <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>

            <span class="c1"># Get layer class</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">layer_class</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span>
                    <span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s2">&quot;keras.layers&quot;</span><span class="p">),</span>
                    <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;class_name&#39;</span><span class="p">]</span>
                <span class="p">)</span>

            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Invalid Keras layer type [</span><span class="si">{type}</span><span class="s1">].&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                    <span class="nb">type</span><span class="o">=</span><span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;class_name&#39;</span><span class="p">]</span>
                <span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

            <span class="c1"># Inject constants</span>
            <span class="k">for</span> <span class="n">config_field</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
                <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">][</span><span class="n">config_field</span><span class="p">]</span> <span class="o">=</span> <span class="n">process_field</span><span class="p">(</span>
                    <span class="n">value</span><span class="o">=</span><span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">][</span><span class="n">config_field</span><span class="p">],</span>
                    <span class="n">constants_dict</span><span class="o">=</span><span class="n">constants</span>
                <span class="p">)</span>

            <span class="c1"># Convert lists into tuples</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">tuple_fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">]:</span>
                    <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">][</span><span class="n">field</span><span class="p">]</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">][</span><span class="n">field</span><span class="p">])</span>

            <span class="c1"># Inject input shape for Input layer if not given</span>
            <span class="k">if</span> <span class="n">layer_id</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">layer_setup</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;config.input_shape&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="c1"># Set input layer dimension for the first layer if not set</span>
                <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">][</span><span class="s1">&#39;input_shape&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">input_shape</span><span class="p">,)</span>

            <span class="k">if</span> <span class="s1">&#39;wrapper&#39;</span> <span class="ow">in</span> <span class="n">layer_setup</span><span class="p">:</span>
                <span class="c1"># Get layer wrapper class</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">wrapper_class</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span>
                        <span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s2">&quot;keras.layers&quot;</span><span class="p">),</span>
                        <span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;wrapper&#39;</span><span class="p">]</span>
                    <span class="p">)</span>

                <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Invalid Keras layer wrapper type [</span><span class="si">{type}</span><span class="s1">].&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                        <span class="nb">type</span><span class="o">=</span><span class="n">layer_setup</span><span class="p">[</span><span class="s1">&#39;wrapper&#39;</span><span class="p">]</span>
                    <span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                    <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

                <span class="n">wrapper_parameters</span> <span class="o">=</span> <span class="n">layer_setup</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;config_wrapper&#39;</span><span class="p">,</span> <span class="p">{})</span>

                <span class="k">if</span> <span class="n">layer_setup</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;config&#39;</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span>
                        <span class="n">wrapper_class</span><span class="p">(</span><span class="n">layer_class</span><span class="p">(</span><span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="n">layer_setup</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;config&#39;</span><span class="p">))),</span> <span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="n">wrapper_parameters</span><span class="p">)))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">wrapper_class</span><span class="p">(</span><span class="n">layer_class</span><span class="p">(),</span> <span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="n">wrapper_parameters</span><span class="p">)))</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">layer_setup</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;config&#39;</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layer_class</span><span class="p">(</span><span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="n">layer_setup</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;config&#39;</span><span class="p">))))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">layer_class</span><span class="p">())</span>

        <span class="c1"># Get Optimizer class</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">optimizer_class</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span>
                <span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s2">&quot;keras.optimizers&quot;</span><span class="p">),</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.optimizer.type&#39;</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Invalid Keras optimizer type [</span><span class="si">{type}</span><span class="s1">].&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="nb">type</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.optimizer.type&#39;</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

        <span class="c1"># Compile the model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
            <span class="n">loss</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.loss&#39;</span><span class="p">),</span>
            <span class="n">optimizer</span><span class="o">=</span><span class="n">optimizer_class</span><span class="p">(</span><span class="o">**</span><span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.optimizer.parameters&#39;</span><span class="p">,</span> <span class="p">{}))),</span>
            <span class="n">metrics</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.metrics&#39;</span><span class="p">)</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="KerasMixin.create_callback_list"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.create_callback_list.html#dcase_framework.keras_utils.KerasMixin.create_callback_list">[docs]</a>    <span class="k">def</span> <span class="nf">create_callback_list</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create list of Keras callbacks</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">callbacks</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Fetch processing interval</span>
        <span class="n">processing_interval</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_processing_interval</span><span class="p">()</span>

        <span class="c1"># Collect all external metrics</span>
        <span class="n">external_metrics</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;validation.enable&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span>
                    <span class="s1">&#39;training.epoch_processing.external_metrics.enable&#39;</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.external_metrics.metrics&#39;</span><span class="p">):</span>
                    <span class="n">current_metric_name</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)</span>
                    <span class="n">current_metric_label</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;label&#39;</span><span class="p">,</span> <span class="n">current_metric_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>

                    <span class="n">external_metrics</span><span class="p">[</span><span class="n">current_metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">current_metric_name</span>

        <span class="c1"># ProgressLoggerCallback</span>
        <span class="kn">from</span> <span class="nn">dcase_framework.keras_utils</span> <span class="k">import</span> <span class="n">ProgressLoggerCallback</span>
        <span class="n">callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">ProgressLoggerCallback</span><span class="p">(</span>
                <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.metrics&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">],</span>
                <span class="n">loss</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.loss&#39;</span><span class="p">),</span>
                <span class="n">disable_progress_bar</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">disable_progress_bar</span><span class="p">,</span>
                <span class="n">log_progress</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span><span class="p">,</span>
                <span class="n">epochs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epochs&#39;</span><span class="p">),</span>
                <span class="n">close_progress_bar</span><span class="o">=</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">),</span>
                <span class="n">manual_update</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">),</span>
                <span class="n">manual_update_interval</span><span class="o">=</span><span class="n">processing_interval</span><span class="p">,</span>
                <span class="n">external_metric_labels</span><span class="o">=</span><span class="n">external_metrics</span>
            <span class="p">)</span>
        <span class="p">)</span>

        <span class="c1"># Add model callbacks</span>
        <span class="k">for</span> <span class="n">cp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.callbacks&#39;</span><span class="p">,</span> <span class="p">[]):</span>
            <span class="n">cp_params</span> <span class="o">=</span> <span class="n">DottedDict</span><span class="p">(</span><span class="n">cp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;parameters&#39;</span><span class="p">,</span> <span class="p">{}))</span>
            <span class="k">if</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Plotter&#39;</span><span class="p">:</span>
                <span class="kn">from</span> <span class="nn">dcase_framework.keras_utils</span> <span class="k">import</span> <span class="n">ProgressPlotterCallback</span>
                <span class="n">callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">ProgressPlotterCallback</span><span class="p">(</span>
                        <span class="n">filename</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">cp_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;output_format&#39;</span><span class="p">,</span> <span class="s1">&#39;pdf&#39;</span><span class="p">),</span>
                        <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.metrics&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">],</span>
                        <span class="n">loss</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;model.loss&#39;</span><span class="p">),</span>
                        <span class="n">disable_progress_bar</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">disable_progress_bar</span><span class="p">,</span>
                        <span class="n">log_progress</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span><span class="p">,</span>
                        <span class="n">epochs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epochs&#39;</span><span class="p">),</span>
                        <span class="n">close_progress_bar</span><span class="o">=</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">),</span>
                        <span class="n">manual_update</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">),</span>
                        <span class="n">interactive</span><span class="o">=</span><span class="n">cp_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;interactive&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">),</span>
                        <span class="n">save</span><span class="o">=</span><span class="n">cp_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;save&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">),</span>
                        <span class="n">focus_span</span><span class="o">=</span><span class="n">cp_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;focus_span&#39;</span><span class="p">),</span>
                        <span class="n">plotting_rate</span><span class="o">=</span><span class="n">cp_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;plotting_rate&#39;</span><span class="p">),</span>
                        <span class="n">external_metric_labels</span><span class="o">=</span><span class="n">external_metrics</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">elif</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Stopper&#39;</span><span class="p">:</span>
                <span class="kn">from</span> <span class="nn">dcase_framework.keras_utils</span> <span class="k">import</span> <span class="n">StopperCallback</span>
                <span class="n">callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">StopperCallback</span><span class="p">(</span>
                        <span class="n">epochs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epochs&#39;</span><span class="p">),</span>
                        <span class="n">manual_update</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">),</span>
                        <span class="n">external_metric_labels</span><span class="o">=</span><span class="n">external_metrics</span><span class="p">,</span>
                        <span class="o">**</span><span class="n">cp_params</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">elif</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Stasher&#39;</span><span class="p">:</span>
                <span class="kn">from</span> <span class="nn">dcase_framework.keras_utils</span> <span class="k">import</span> <span class="n">StasherCallback</span>
                <span class="n">callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">StasherCallback</span><span class="p">(</span>
                        <span class="n">epochs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epochs&#39;</span><span class="p">),</span>
                        <span class="n">manual_update</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">),</span>
                        <span class="n">external_metric_labels</span><span class="o">=</span><span class="n">external_metrics</span><span class="p">,</span>
                        <span class="o">**</span><span class="n">cp_params</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Keras standard callbacks</span>
                <span class="k">if</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;ModelCheckpoint&#39;</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;parameters&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;filepath&#39;</span><span class="p">):</span>
                    <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;parameters&#39;</span><span class="p">][</span><span class="s1">&#39;filepath&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> \
                                                   <span class="s1">&#39;.weights.</span><span class="si">{epoch:02d}</span><span class="s1">-</span><span class="si">{val_loss:.2f}</span><span class="s1">.hdf5&#39;</span>

                <span class="k">if</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;EarlyStopping&#39;</span> <span class="ow">and</span> <span class="n">cp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;parameters&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;monitor&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;val_&#39;</span><span class="p">)</span> \
                        <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;validation.enable&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>

                    <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Cannot use callback type [</span><span class="si">{type}</span><span class="s1">] with monitor parameter [</span><span class="si">{monitor}</span><span class="s1">] &#39;</span> \
                              <span class="s1">&#39;as there is no validation set.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                                                                      <span class="nb">type</span><span class="o">=</span><span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">],</span>
                                                                      <span class="n">monitor</span><span class="o">=</span><span class="n">cp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;parameters&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;monitor&#39;</span><span class="p">)</span>
                                                                      <span class="p">)</span>

                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                    <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

                <span class="k">try</span><span class="p">:</span>
                    <span class="n">callback_class</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s2">&quot;keras.callbacks&quot;</span><span class="p">),</span> <span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">])</span>
                    <span class="n">callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">callback_class</span><span class="p">(</span><span class="o">**</span><span class="n">cp_params</span><span class="p">))</span>

                <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Invalid Keras callback type [</span><span class="si">{type}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                        <span class="nb">type</span><span class="o">=</span><span class="n">cp</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
                    <span class="p">)</span>

                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                    <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">callbacks</span></div>

<div class="viewcode-block" id="KerasMixin.create_external_metric_evaluators"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.create_external_metric_evaluators.html#dcase_framework.keras_utils.KerasMixin.create_external_metric_evaluators">[docs]</a>    <span class="k">def</span> <span class="nf">create_external_metric_evaluators</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create external metric evaluators</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Initialize external metrics</span>
        <span class="n">external_metric_evaluators</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;validation.enable&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span>
                    <span class="s1">&#39;training.epoch_processing.external_metrics.enable&#39;</span><span class="p">):</span>
                <span class="kn">import</span> <span class="nn">sed_eval</span>

                <span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.external_metrics.metrics&#39;</span><span class="p">):</span>
                    <span class="c1"># Current metric info</span>
                    <span class="n">current_metric_evaluator</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;evaluator&#39;</span><span class="p">)</span>
                    <span class="n">current_metric_name</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)</span>
                    <span class="n">current_metric_params</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;parameters&#39;</span><span class="p">,</span> <span class="p">{})</span>
                    <span class="n">current_metric_label</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;label&#39;</span><span class="p">,</span> <span class="n">current_metric_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>

                    <span class="c1"># Initialize sed_eval evaluators</span>
                    <span class="k">if</span> <span class="n">current_metric_evaluator</span> <span class="o">==</span> <span class="s1">&#39;sed_eval.scene&#39;</span><span class="p">:</span>
                        <span class="n">evaluator</span> <span class="o">=</span> <span class="n">sed_eval</span><span class="o">.</span><span class="n">scene</span><span class="o">.</span><span class="n">SceneClassificationMetrics</span><span class="p">(</span>
                            <span class="n">scene_labels</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="p">,</span>
                            <span class="o">**</span><span class="n">current_metric_params</span>
                        <span class="p">)</span>

                    <span class="k">elif</span> <span class="p">(</span><span class="n">current_metric_evaluator</span> <span class="o">==</span> <span class="s1">&#39;sed_eval.segment_based&#39;</span> <span class="ow">or</span>
                          <span class="n">current_metric_evaluator</span> <span class="o">==</span> <span class="s1">&#39;sed_eval.sound_event.segment_based&#39;</span><span class="p">):</span>
                        <span class="n">evaluator</span> <span class="o">=</span> <span class="n">sed_eval</span><span class="o">.</span><span class="n">sound_event</span><span class="o">.</span><span class="n">SegmentBasedMetrics</span><span class="p">(</span>
                            <span class="n">event_label_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="p">,</span>
                            <span class="o">**</span><span class="n">current_metric_params</span>
                        <span class="p">)</span>

                    <span class="k">elif</span> <span class="p">(</span><span class="n">current_metric_evaluator</span> <span class="o">==</span> <span class="s1">&#39;sed_eval.event_based&#39;</span> <span class="ow">or</span>
                          <span class="n">current_metric_evaluator</span> <span class="o">==</span> <span class="s1">&#39;sed_eval.sound_event.event_based&#39;</span><span class="p">):</span>
                        <span class="n">evaluator</span> <span class="o">=</span> <span class="n">sed_eval</span><span class="o">.</span><span class="n">sound_event</span><span class="o">.</span><span class="n">EventBasedMetrics</span><span class="p">(</span>
                            <span class="n">event_label_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="p">,</span>
                            <span class="o">**</span><span class="n">current_metric_params</span>
                        <span class="p">)</span>

                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Unknown target metric [</span><span class="si">{metric}</span><span class="s1">].&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                            <span class="n">metric</span><span class="o">=</span><span class="n">current_metric_name</span>
                        <span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                        <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

                    <span class="c1"># Check evaluator API</span>
                    <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">evaluator</span><span class="p">,</span> <span class="s1">&#39;reset&#39;</span><span class="p">)</span> <span class="ow">or</span>
                       <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">evaluator</span><span class="p">,</span> <span class="s1">&#39;evaluate&#39;</span><span class="p">)</span> <span class="ow">or</span>
                       <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">evaluator</span><span class="p">,</span> <span class="s1">&#39;results&#39;</span><span class="p">)):</span>
                        <span class="k">if</span> <span class="n">current_metric_evaluator</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;sed_eval&#39;</span><span class="p">):</span>
                            <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: wrong version of sed_eval for [</span><span class="si">{current_metric_evaluator}</span><span class="s1">::</span><span class="si">{current_metric_name}</span><span class="s1">], update sed_eval to latest version&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                                <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                                <span class="n">current_metric_evaluator</span><span class="o">=</span><span class="n">current_metric_evaluator</span><span class="p">,</span>
                                <span class="n">current_metric_name</span><span class="o">=</span><span class="n">current_metric_name</span>
                            <span class="p">)</span>

                            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Evaluator has invalid API [</span><span class="si">{current_metric_evaluator}</span><span class="s1">::</span><span class="si">{current_metric_name}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                                <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                                <span class="n">current_metric_evaluator</span><span class="o">=</span><span class="n">current_metric_evaluator</span><span class="p">,</span>
                                <span class="n">current_metric_name</span><span class="o">=</span><span class="n">current_metric_name</span>
                            <span class="p">)</span>

                            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

                    <span class="c1"># Form unique name for metric, to allow multiple similar metrics with different parameters</span>
                    <span class="n">metric_id</span> <span class="o">=</span> <span class="n">get_parameter_hash</span><span class="p">(</span><span class="n">metric</span><span class="p">)</span>

                    <span class="c1"># Metric data container</span>
                    <span class="n">metric_data</span> <span class="o">=</span> <span class="p">{</span>
                        <span class="s1">&#39;evaluator_name&#39;</span><span class="p">:</span> <span class="n">current_metric_evaluator</span><span class="p">,</span>
                        <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">current_metric_name</span><span class="p">,</span>
                        <span class="s1">&#39;params&#39;</span><span class="p">:</span> <span class="n">current_metric_params</span><span class="p">,</span>
                        <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="n">current_metric_label</span><span class="p">,</span>
                        <span class="s1">&#39;path&#39;</span><span class="p">:</span> <span class="n">current_metric_name</span><span class="p">,</span>
                        <span class="s1">&#39;evaluator&#39;</span><span class="p">:</span> <span class="n">evaluator</span><span class="p">,</span>
                    <span class="p">}</span>
                    <span class="n">external_metric_evaluators</span><span class="p">[</span><span class="n">metric_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_data</span>

        <span class="k">return</span> <span class="n">external_metric_evaluators</span></div>

<div class="viewcode-block" id="KerasMixin.get_processing_interval"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.KerasMixin.get_processing_interval.html#dcase_framework.keras_utils.KerasMixin.get_processing_interval">[docs]</a>    <span class="k">def</span> <span class="nf">get_processing_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Processing interval</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">processing_interval</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.enable&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;training.epoch_processing.external_metrics.enable&#39;</span><span class="p">):</span>
                <span class="n">processing_interval</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span>
                    <span class="s1">&#39;training.epoch_processing.external_metrics.evaluation_interval&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">processing_interval</span></div>

    <span class="k">def</span> <span class="nf">_after_load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_return</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">with</span> <span class="n">SuppressStdoutAndStderr</span><span class="p">():</span>
            <span class="c1"># Setup Keras if not yet set up. This is needed as keras has tensorflow as default backend, and this will</span>
            <span class="c1"># give error if it is not installed and theano is not set up as backend.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_setup_keras</span><span class="p">()</span>

            <span class="kn">from</span> <span class="nn">keras.models</span> <span class="k">import</span> <span class="n">load_model</span>

        <span class="n">keras_model_filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;.model.hdf5&#39;</span>

        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">keras_model_filename</span><span class="p">):</span>
            <span class="k">with</span> <span class="n">SuppressStdoutAndStderr</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">load_model</span><span class="p">(</span><span class="n">keras_model_filename</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Keras model not found [</span><span class="si">{filename}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">filename</span><span class="o">=</span><span class="n">keras_model_filename</span>
            <span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_after_save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_return</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Save keras model and weight</span>
        <span class="n">keras_model_filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;.model.hdf5&#39;</span>
        <span class="n">model_weights_filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;.weights.hdf5&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">keras_model_filename</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save_weights</span><span class="p">(</span><span class="n">model_weights_filename</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_setup_keras</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Setup keras backend and parameters</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;keras_setup_done&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">keras_setup_done</span><span class="p">:</span>
            <span class="c1"># Get BLAS library associated to numpy</span>
            <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">__config__</span><span class="o">.</span><span class="n">blas_opt_info</span> <span class="ow">and</span> <span class="s1">&#39;libraries&#39;</span> <span class="ow">in</span> <span class="n">numpy</span><span class="o">.</span><span class="n">__config__</span><span class="o">.</span><span class="n">blas_opt_info</span><span class="p">:</span>
                <span class="n">blas_libraries</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">__config__</span><span class="o">.</span><span class="n">blas_opt_info</span><span class="p">[</span><span class="s1">&#39;libraries&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">blas_libraries</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">]</span>

            <span class="n">blas_extra_info</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="c1"># Set backend and parameters before importing keras</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  Keras&#39;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    Backend </span><span class="se">\t</span><span class="s1">[</span><span class="si">{backend}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">backend</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend&#39;</span><span class="p">,</span> <span class="s1">&#39;theano&#39;</span><span class="p">))</span>
                <span class="p">)</span>

            <span class="c1"># Threading</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.threads&#39;</span><span class="p">):</span>
                <span class="n">thread_count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.threads&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
                <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;GOTO_NUM_THREADS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">thread_count</span><span class="p">)</span>
                <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;OMP_NUM_THREADS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">thread_count</span><span class="p">)</span>
                <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;MKL_NUM_THREADS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">thread_count</span><span class="p">)</span>
                <span class="n">blas_extra_info</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;Threads[</span><span class="si">{threads}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">threads</span><span class="o">=</span><span class="n">thread_count</span><span class="p">))</span>

                <span class="k">if</span> <span class="n">thread_count</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;OMP_DYNAMIC&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;False&#39;</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;MKL_DYNAMIC&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;False&#39;</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;OMP_DYNAMIC&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;True&#39;</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;MKL_DYNAMIC&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;True&#39;</span>

            <span class="c1"># Conditional Numerical Reproducibility (CNR) for MKL BLAS library</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.CNR&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span> <span class="ow">and</span> <span class="n">blas_libraries</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;mkl&#39;</span><span class="p">):</span>
                <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;MKL_CBWR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;COMPATIBLE&#39;</span>
                <span class="n">blas_extra_info</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;MKL_CBWR[</span><span class="si">{mode}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">&#39;COMPATIBLE&#39;</span><span class="p">))</span>

            <span class="c1"># Show BLAS info</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">__config__</span><span class="o">.</span><span class="n">blas_opt_info</span> <span class="ow">and</span> <span class="s1">&#39;libraries&#39;</span> <span class="ow">in</span> <span class="n">numpy</span><span class="o">.</span><span class="n">__config__</span><span class="o">.</span><span class="n">blas_opt_info</span><span class="p">:</span>
                    <span class="n">blas_libraries</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">__config__</span><span class="o">.</span><span class="n">blas_opt_info</span><span class="p">[</span><span class="s1">&#39;libraries&#39;</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">blas_libraries</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;openblas&#39;</span><span class="p">):</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    BLAS library</span><span class="se">\t</span><span class="s1">[OpenBLAS]</span><span class="se">\t\t</span><span class="s1">(</span><span class="si">{info}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">info</span><span class="o">=</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">blas_extra_info</span><span class="p">))</span>
                        <span class="p">)</span>

                    <span class="k">elif</span> <span class="n">blas_libraries</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;blas&#39;</span><span class="p">):</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                            <span class="s1">&#39;  BLAS library</span><span class="se">\t</span><span class="s1">[BLAS/Atlas]</span><span class="se">\t\t</span><span class="s1">(</span><span class="si">{info}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                                <span class="n">info</span><span class="o">=</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">blas_extra_info</span><span class="p">))</span>
                        <span class="p">)</span>

                    <span class="k">elif</span> <span class="n">blas_libraries</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;mkl&#39;</span><span class="p">):</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    BLAS library</span><span class="se">\t</span><span class="s1">[MKL]</span><span class="se">\t\t</span><span class="s1">(</span><span class="si">{info}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">info</span><span class="o">=</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">blas_extra_info</span><span class="p">))</span>
                        <span class="p">)</span>

            <span class="c1"># Select Keras backend</span>
            <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;KERAS_BACKEND&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend&#39;</span><span class="p">,</span> <span class="s1">&#39;theano&#39;</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend&#39;</span><span class="p">,</span> <span class="s1">&#39;theano&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;theano&#39;</span><span class="p">:</span>
                <span class="c1"># Theano setup</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  Theano&#39;</span><span class="p">)</span>
                <span class="c1"># Default flags</span>
                <span class="n">flags</span> <span class="o">=</span> <span class="p">[</span>
                    <span class="c1"># &#39;ldflags=&#39;,</span>
                    <span class="s1">&#39;warn.round=False&#39;</span><span class="p">,</span>
                <span class="p">]</span>

                <span class="c1"># Set device</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">):</span>
                    <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;device=&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">,</span> <span class="s1">&#39;cpu&#39;</span><span class="p">))</span>

                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    Device </span><span class="se">\t\t</span><span class="s1">[</span><span class="si">{device}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">device</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">,</span> <span class="s1">&#39;cpu&#39;</span><span class="p">))</span>
                        <span class="p">)</span>

                <span class="c1"># Set floatX</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.floatX&#39;</span><span class="p">):</span>
                    <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;floatX=&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.floatX&#39;</span><span class="p">,</span> <span class="s1">&#39;float32&#39;</span><span class="p">))</span>

                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    floatX </span><span class="se">\t\t</span><span class="s1">[</span><span class="si">{float}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="nb">float</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.floatX&#39;</span><span class="p">,</span> <span class="s1">&#39;float32&#39;</span><span class="p">))</span>
                        <span class="p">)</span>

                <span class="c1"># Set optimizer</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.optimizer&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.optimizer&#39;</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;fast_run&#39;</span><span class="p">,</span> <span class="s1">&#39;merge&#39;</span><span class="p">,</span>
                                                                                              <span class="s1">&#39;fast_compile&#39;</span><span class="p">,</span> <span class="s1">&#39;None&#39;</span><span class="p">]:</span>
                        <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;optimizer=&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.optimizer&#39;</span><span class="p">))</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    Optimizer </span><span class="se">\t</span><span class="s1">[</span><span class="si">{optimizer}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">optimizer</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.optimizer&#39;</span><span class="p">,</span> <span class="s1">&#39;None&#39;</span><span class="p">))</span>
                    <span class="p">)</span>

                <span class="c1"># Set fastmath for GPU mode only</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.fastmath&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span>
                        <span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">,</span> <span class="s1">&#39;cpu&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;cpu&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.fastmath&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
                        <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;nvcc.fastmath=True&#39;</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;nvcc.fastmath=False&#39;</span><span class="p">)</span>

                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    NVCC fastmath </span><span class="se">\t</span><span class="s1">[</span><span class="si">{flag}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">flag</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.fastmath&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)))</span>
                        <span class="p">)</span>

                <span class="c1"># Set OpenMP</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.openmp&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.openmp&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
                        <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;openmp=True&#39;</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">flags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;openmp=False&#39;</span><span class="p">)</span>

                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    OpenMP</span><span class="se">\t\t</span><span class="s1">[</span><span class="si">{flag}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">flag</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.openmp&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)))</span>
                        <span class="p">)</span>

                <span class="c1"># Set environmental variable for Theano</span>
                <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;THEANO_FLAGS&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">flags</span><span class="p">)</span>

            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend&#39;</span><span class="p">,</span> <span class="s1">&#39;tensorflow&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;tensorflow&#39;</span><span class="p">:</span>
                <span class="c1"># Tensorflow setup</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  Tensorflow&#39;</span><span class="p">)</span>
                <span class="c1"># Set device</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">,</span> <span class="s1">&#39;cpu&#39;</span><span class="p">):</span>

                    <span class="c1"># In case of CPU disable visible GPU.</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">,</span> <span class="s1">&#39;cpu&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;cpu&#39;</span><span class="p">:</span>
                        <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;CUDA_VISIBLE_DEVICES&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    Device </span><span class="se">\t\t</span><span class="s1">[</span><span class="si">{device}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="n">device</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend_parameters.device&#39;</span><span class="p">,</span> <span class="s1">&#39;cpu&#39;</span><span class="p">)))</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Keras backend not supported [backend].&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                    <span class="n">backend</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">learner_params</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s1">&#39;keras.backend&#39;</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">AssertionError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show_extra_debug</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  &#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">keras_setup_done</span> <span class="o">=</span> <span class="kc">True</span>


<div class="viewcode-block" id="BaseCallback"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.BaseCallback.html#dcase_framework.keras_utils.BaseCallback">[docs]</a><span class="k">class</span> <span class="nc">BaseCallback</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for Callbacks</span>
<span class="sd">    &quot;&quot;&quot;</span>
<div class="viewcode-block" id="BaseCallback.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.BaseCallback.html#dcase_framework.keras_utils.BaseCallback.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">params</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;verbose&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">manual_update</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;manual_update&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;epochs&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;external_metric_labels&#39;</span><span class="p">,</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">external_metric</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">keras_metrics</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s1">&#39;binary_accuracy&#39;</span><span class="p">,</span>
            <span class="s1">&#39;categorical_accuracy&#39;</span><span class="p">,</span>
            <span class="s1">&#39;sparse_categorical_accuracy&#39;</span><span class="p">,</span>
            <span class="s1">&#39;top_k_categorical_accuracy&#39;</span>
        <span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">set_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>

    <span class="k">def</span> <span class="nf">set_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">params</span> <span class="o">=</span> <span class="n">params</span>

    <span class="k">def</span> <span class="nf">on_train_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_train_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_epoch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_batch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_batch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">add_external_metric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">set_external_metric_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">,</span> <span class="n">metric_value</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">get_operator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric</span><span class="p">):</span>
        <span class="n">metric</span> <span class="o">=</span> <span class="n">metric</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;error_rate&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;er&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">less</span>

        <span class="k">elif</span> <span class="p">(</span><span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_measure&#39;</span><span class="p">)</span> <span class="ow">or</span>
             <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;fmeasure&#39;</span><span class="p">)</span> <span class="ow">or</span>
             <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;fscore&#39;</span><span class="p">)</span> <span class="ow">or</span>
             <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f-score&#39;</span><span class="p">)):</span>

            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span>

        <span class="k">elif</span> <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;accuracy&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">metric</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;acc&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">less</span></div>


<div class="viewcode-block" id="ProgressLoggerCallback"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.ProgressLoggerCallback.html#dcase_framework.keras_utils.ProgressLoggerCallback">[docs]</a><span class="k">class</span> <span class="nc">ProgressLoggerCallback</span><span class="p">(</span><span class="n">BaseCallback</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Keras callback to store metrics with tqdm progress bar or logging interface. Implements Keras Callback API.</span>

<span class="sd">    This callback is very similar to standard ``ProgbarLogger`` Keras callback, however it adds support for logging</span>
<span class="sd">    interface and tqdm based progress bars, and external metrics (metrics calculated outside Keras training process).</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="ProgressLoggerCallback.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.ProgressLoggerCallback.html#dcase_framework.keras_utils.ProgressLoggerCallback.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        epochs : int</span>
<span class="sd">            Total amount of epochs</span>

<span class="sd">        metric : str</span>
<span class="sd">            Metric name</span>

<span class="sd">        manual_update : bool</span>
<span class="sd">            Manually update callback, use this to when injecting external metrics</span>
<span class="sd">            Default value True</span>

<span class="sd">        manual_update_interval : int</span>
<span class="sd">            Epoch interval for manual update, used anticipate updates</span>
<span class="sd">            Default value 1</span>

<span class="sd">        disable_progress_bar : bool</span>
<span class="sd">            Disable tqdm based progress bar</span>
<span class="sd">            Default value False</span>

<span class="sd">        close_progress_bar : bool</span>
<span class="sd">            Close tqdm progress bar on training end</span>
<span class="sd">            Default value True</span>

<span class="sd">        log_progress : bool</span>
<span class="sd">            Print progress into logging interface</span>
<span class="sd">            Default value False</span>

<span class="sd">        external_metric_labels : dict or OrderedDict</span>
<span class="sd">            Dictionary with</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ProgressLoggerCallback</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;metric&#39;</span><span class="p">),</span> <span class="nb">str</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;metric&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">callable</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;metric&#39;</span><span class="p">)):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;metric&#39;</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">loss</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;loss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disable_progress_bar</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;disable_progress_bar&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">close_progress_bar</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;close_progress_bar&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">manual_update_interval</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;manual_update_interval&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;log_progress&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timer</span> <span class="o">=</span> <span class="n">Timer</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">validation_data</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seen</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">log_values</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;l_tra&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
            <span class="s1">&#39;l_val&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
            <span class="s1">&#39;m_tra&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
            <span class="s1">&#39;m_val&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

        <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">header_show</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="kc">None</span></div>

    <span class="k">def</span> <span class="nf">on_train_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;epochs&#39;</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">header_show</span><span class="p">:</span>
            <span class="c1"># Show header only once</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">header_show</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;  Training&#39;</span><span class="p">)</span>
            <span class="n">header_extra1</span> <span class="o">=</span> <span class="s1">&#39;    </span><span class="si">{epoch:&lt;5s}</span><span class="s1"> | </span><span class="si">{loss:&lt;19s}</span><span class="s1"> | </span><span class="si">{metric:&lt;19s}</span><span class="s1"> | &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">epoch</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">5</span><span class="p">,</span>
                <span class="n">loss</span><span class="o">=</span><span class="s1">&#39;Loss&#39;</span><span class="p">,</span>
                <span class="n">metric</span><span class="o">=</span><span class="s1">&#39;Metric&#39;</span><span class="p">,</span>
                <span class="n">validation</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                <span class="n">line</span> <span class="o">=</span> <span class="s1">&#39;{external_value:&lt;&#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="mi">12</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">))</span><span class="o">+</span><span class="s1">&#39;s} &#39;</span>
                <span class="n">header_extra1</span> <span class="o">+=</span> <span class="n">line</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">external_value</span><span class="o">=</span><span class="s1">&#39;External metric&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="n">header_extra1</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{time:&lt;15s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">time</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">15</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">loss_label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">loss_label</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">19</span><span class="p">:</span>
                <span class="n">loss_label</span> <span class="o">=</span> <span class="n">loss_label</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">17</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;..&#39;</span>

            <span class="n">metric_label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">metric_label</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">19</span><span class="p">:</span>
                <span class="n">metric_label</span> <span class="o">=</span> <span class="n">metric_label</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">17</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;..&#39;</span>

            <span class="n">header_extra2</span> <span class="o">=</span> <span class="s1">&#39;    </span><span class="si">{epoch:&lt;5s}</span><span class="s1"> | </span><span class="si">{loss:&lt;19s}</span><span class="s1"> | </span><span class="si">{metric:&lt;19s}</span><span class="s1"> | &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">epoch</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">5</span><span class="p">,</span>
                <span class="n">loss</span><span class="o">=</span><span class="n">loss_label</span><span class="p">,</span>
                <span class="n">metric</span><span class="o">=</span><span class="n">metric_label</span><span class="p">,</span>
                <span class="n">validation</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                    <span class="n">header_extra2</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{label:&lt;10s}</span><span class="s1"> | &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">metric_label</span><span class="p">)</span>

            <span class="n">header_extra2</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{time:&lt;15s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">time</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">15</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="n">header_main</span> <span class="o">=</span> <span class="s1">&#39;    </span><span class="si">{epoch:&lt;5s}</span><span class="s1"> | </span><span class="si">{loss:&lt;8s}</span><span class="s1"> | </span><span class="si">{val_loss:&lt;8s}</span><span class="s1"> | </span><span class="si">{train:&lt;8s}</span><span class="s1"> | </span><span class="si">{validation:&lt;8s}</span><span class="s1"> | &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">epoch</span><span class="o">=</span><span class="s1">&#39;Epoch&#39;</span><span class="p">,</span>
                <span class="n">loss</span><span class="o">=</span><span class="s1">&#39;Train&#39;</span><span class="p">,</span>
                <span class="n">val_loss</span><span class="o">=</span><span class="s1">&#39;Val&#39;</span><span class="p">,</span>
                <span class="n">train</span><span class="o">=</span><span class="s1">&#39;Train&#39;</span><span class="p">,</span>
                <span class="n">validation</span><span class="o">=</span><span class="s1">&#39;Val&#39;</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                    <span class="n">header_main</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{label:&lt;10s}</span><span class="s1"> | &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s1">&#39;Val&#39;</span><span class="p">)</span>

            <span class="n">header_main</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{time:&lt;15s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">time</span><span class="o">=</span><span class="s1">&#39;Time&#39;</span>
            <span class="p">)</span>

            <span class="n">sep</span> <span class="o">=</span> <span class="s1">&#39;    </span><span class="si">{epoch:&lt;5s}</span><span class="s1"> + </span><span class="si">{loss:&lt;8s}</span><span class="s1"> + </span><span class="si">{val_loss:&lt;8s}</span><span class="s1"> + </span><span class="si">{train:&lt;8s}</span><span class="s1"> + </span><span class="si">{validation:&lt;8s}</span><span class="s1"> + &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">epoch</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">5</span><span class="p">,</span>
                <span class="n">loss</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span>
                <span class="n">val_loss</span><span class="o">=</span><span class="s1">&#39;-&#39;</span> <span class="o">*</span> <span class="mi">8</span><span class="p">,</span>
                <span class="n">train</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span>
                <span class="n">validation</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                    <span class="n">sep</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{external_value:&lt;10s}</span><span class="s1"> + &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">external_value</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span>
                    <span class="p">)</span>
            <span class="n">sep</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{time:&lt;15s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">time</span><span class="o">=</span><span class="s1">&#39;-&#39;</span><span class="o">*</span><span class="mi">15</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">header_extra1</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">header_extra2</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">header_main</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>

        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span> <span class="o">=</span> <span class="n">tqdm</span><span class="p">(</span><span class="n">total</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,</span>
                                     <span class="n">initial</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span>
                                     <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span>
                                     <span class="n">desc</span><span class="o">=</span><span class="s1">&#39;    </span><span class="si">{0:&gt;6s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;Learn&#39;</span><span class="p">),</span>
                                     <span class="n">leave</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                                     <span class="n">miniters</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                                     <span class="n">disable</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">disable_progress_bar</span>
                                     <span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_train_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">close_progress_bar</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">on_epoch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span> <span class="o">+</span> <span class="mi">1</span>

        <span class="k">if</span> <span class="s1">&#39;steps&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;steps&#39;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="s1">&#39;samples&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">target</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;samples&#39;</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">seen</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">on_batch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">seen</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log_values</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">on_batch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">logs</span> <span class="o">=</span> <span class="n">logs</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="n">batch_size</span> <span class="o">=</span> <span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;size&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seen</span> <span class="o">+=</span> <span class="n">batch_size</span>

        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;metrics&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">logs</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log_values</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>

    <span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timer</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span>

        <span class="n">logs</span> <span class="o">=</span> <span class="n">logs</span> <span class="ow">or</span> <span class="p">{}</span>

        <span class="c1"># Reset values</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Collect values</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;metrics&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">logs</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log_values</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>
                <span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;loss&#39;</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:4.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>

                <span class="k">elif</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;val_loss&#39;</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:4.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>

                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric</span> <span class="ow">and</span> <span class="n">k</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;val_&#39;</span><span class="p">):</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:4.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:4.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>

        <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric</span><span class="p">:</span>
                <span class="n">metric_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span>
                <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">metric_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_measure&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">metric_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_score&#39;</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:3.1f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">value</span><span class="o">*</span><span class="mi">100</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:4.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">manual_update</span> <span class="ow">or</span>
           <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">manual_update_interval</span><span class="p">)):</span>
            <span class="c1"># Update logged progress</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">update_progress_log</span><span class="p">()</span>

        <span class="c1"># Increase iteration count and update progress bar</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update_progress_bar</span><span class="p">(</span><span class="n">increase</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update_progress_log</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update_progress_bar</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span>

    <span class="k">def</span> <span class="nf">update_progress_log</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update progress to logging interface</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span><span class="p">:</span>
            <span class="n">output</span> <span class="o">=</span> <span class="s1">&#39;    &#39;</span>
            <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="si">{epoch:&lt;5s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">epoch</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:d}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">))</span>

            <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{loss:&lt;8s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">loss</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:4.6f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]))</span>

            <span class="k">if</span> <span class="s1">&#39;l_val&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">:</span>
                <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{val_loss:&lt;8s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">val_loss</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:4.6f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{val_loss:&lt;8s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">val_loss</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">)</span>

            <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{train:&lt;8s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">train</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:4.6f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]))</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">]:</span>
                <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{validation:&lt;8s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">validation</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:4.6f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{validation:&lt;8s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">validation</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">8</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span>

                    <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
                        <span class="n">value</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">10</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">if</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_measure&#39;</span><span class="p">)</span> <span class="ow">or</span>
                           <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_score&#39;</span><span class="p">)):</span>
                            <span class="n">value</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:3.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">value</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{:4.3f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>

                    <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{external_value:&lt;10s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">external_value</span><span class="o">=</span><span class="n">value</span>
                    <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{external_value:&lt;10s}</span><span class="s1"> |&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">external_value</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">*</span><span class="mi">10</span>
                    <span class="p">)</span>
            <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39; </span><span class="si">{time:&lt;15s}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">time</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">timer</span><span class="o">.</span><span class="n">get_string</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">update_progress_bar</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">increase</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update progress to tqdm progress bar</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span><span class="o">.</span><span class="n">set_postfix</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">postfix</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">increase</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_external_metric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add external metric to be monitored</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        metric_id : str</span>
<span class="sd">            Metric name</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">metric_id</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">[</span><span class="n">metric_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_id</span>

        <span class="k">if</span> <span class="n">metric_id</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_id</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

    <span class="k">def</span> <span class="nf">set_external_metric_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">,</span> <span class="n">metric_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add external metric value</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        metric_label : str</span>
<span class="sd">            Metric label</span>

<span class="sd">        metric_value : numeric</span>
<span class="sd">            Metric value</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">external_metric</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_value</span>

    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Manually close progress logging</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_progress</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">close_progress_bar</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">progress_bar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>


<div class="viewcode-block" id="ProgressPlotterCallback"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.ProgressPlotterCallback.html#dcase_framework.keras_utils.ProgressPlotterCallback">[docs]</a><span class="k">class</span> <span class="nc">ProgressPlotterCallback</span><span class="p">(</span><span class="n">ProgressLoggerCallback</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Keras callback to plot progress during the training process and save final progress into figure.</span>
<span class="sd">    Implements Keras Callback API.</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="ProgressPlotterCallback.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.ProgressPlotterCallback.html#dcase_framework.keras_utils.ProgressPlotterCallback.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        epochs : int</span>
<span class="sd">            Total amount of epochs</span>

<span class="sd">        metric : str</span>
<span class="sd">            Metric name</span>

<span class="sd">        manual_update : bool</span>
<span class="sd">            Manually update callback, use this to when injecting external metrics</span>
<span class="sd">            Default value True</span>

<span class="sd">        interactive : bool</span>
<span class="sd">            Show plot during the training and update with plotting rate</span>
<span class="sd">            Default value True</span>

<span class="sd">        plotting_rate : int</span>
<span class="sd">            Plot update rate in seconds</span>
<span class="sd">            Default value 10</span>

<span class="sd">        save : bool</span>
<span class="sd">            Save plot on disk, plotting rate applies</span>

<span class="sd">        filename : str</span>
<span class="sd">            Filename of figure</span>
<span class="sd">            Default value 1</span>

<span class="sd">        focus_span : int</span>
<span class="sd">            Epoch amount to highlight, and show separately in the plot.</span>
<span class="sd">            Default value 10</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ProgressPlotterCallback</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;filename&#39;</span><span class="p">)</span>

        <span class="c1"># Get file format for the output plot</span>
        <span class="n">file_extension</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">file_extension</span> <span class="o">==</span> <span class="s1">&#39;.eps&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">format</span> <span class="o">=</span> <span class="s1">&#39;eps&#39;</span>
        <span class="k">elif</span> <span class="n">file_extension</span> <span class="o">==</span> <span class="s1">&#39;.svg&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">format</span> <span class="o">=</span> <span class="s1">&#39;svg&#39;</span>
        <span class="k">elif</span> <span class="n">file_extension</span> <span class="o">==</span> <span class="s1">&#39;.pdf&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">format</span> <span class="o">=</span> <span class="s1">&#39;pdf&#39;</span>
        <span class="k">elif</span> <span class="n">file_extension</span> <span class="o">==</span> <span class="s1">&#39;.png&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">format</span> <span class="o">=</span> <span class="s1">&#39;png&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">plotting_rate</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;plotting_rate&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">interactive</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;interactive&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">save</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;save&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">focus_span</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;focus_span&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">focus_span</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">focus_span</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;l_tra&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
            <span class="s1">&#39;l_val&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
            <span class="s1">&#39;m_tra&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
            <span class="s1">&#39;m_val&#39;</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,)),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

        <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
        <span class="kn">import</span> <span class="nn">warnings</span>
        <span class="kn">import</span> <span class="nn">matplotlib.cbook</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">filterwarnings</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">cbook</span><span class="o">.</span><span class="n">mplDeprecation</span><span class="p">)</span>

        <span class="n">figure_height</span> <span class="o">=</span> <span class="mi">8</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">figure_height</span> <span class="o">=</span> <span class="mi">8</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">figure</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">num</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="n">figure_height</span><span class="p">),</span> <span class="n">dpi</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s1">&#39;k&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">interactive</span><span class="p">:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="n">block</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">pause</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Draw plot</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="kn">import</span> <span class="nn">matplotlib.patches</span> <span class="k">as</span> <span class="nn">patches</span>
        <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">number</span><span class="p">)</span>
        <span class="n">row_count</span> <span class="o">=</span> <span class="mi">2</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">((</span><span class="n">row_count</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">rowspan</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">((</span><span class="n">row_count</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">rowspan</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">((</span><span class="n">row_count</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">rowspan</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">((</span><span class="n">row_count</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">rowspan</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">row_id</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="k">for</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">((</span><span class="n">row_count</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="n">row_id</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">rowspan</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot2grid</span><span class="p">((</span><span class="n">row_count</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="n">row_id</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">rowspan</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">colspan</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">row_id</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="n">span</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">focus_span</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="c1"># PLOT 1 / Main</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">cla</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;Loss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Model Loss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span>
            <span class="n">patches</span><span class="o">.</span><span class="n">Rectangle</span><span class="p">(</span>
                <span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()[</span><span class="mi">0</span><span class="p">]),</span>  <span class="c1"># (x,y)</span>
                <span class="n">width</span><span class="o">=</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                <span class="n">height</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()[</span><span class="mi">1</span><span class="p">],</span>
                <span class="n">facecolor</span><span class="o">=</span><span class="s2">&quot;#000000&quot;</span><span class="p">,</span>
                <span class="n">alpha</span><span class="o">=</span><span class="mf">0.05</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="c1"># Horizontal lines</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">numpy</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">])):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">numpy</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">]),</span> <span class="n">lw</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;--&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">numpy</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">]),</span> <span class="n">lw</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;--&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">legend</span><span class="p">([</span><span class="s1">&#39;Train&#39;</span><span class="p">,</span> <span class="s1">&#39;Validation&#39;</span><span class="p">],</span> <span class="n">loc</span><span class="o">=</span><span class="s1">&#39;upper right&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">([])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_1</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

        <span class="c1"># PLOT 1 / Highlighted area</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">cla</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;Loss / Highlighted area&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Model Loss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">][</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">][</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">([])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">tick_right</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax1_2</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_label_position</span><span class="p">(</span><span class="s2">&quot;right&quot;</span><span class="p">)</span>

        <span class="c1"># PLOT 2 / Main</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">cla</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;Metric&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">)</span>
        <span class="c1"># Plots</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="c1"># Horizontal lines</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">numpy</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">])):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_operator</span><span class="p">(</span><span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">)</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span><span class="p">:</span>
                <span class="n">h_tra_line_y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nanmax</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">])</span>
                <span class="n">h_val_line_y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nanmax</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">h_tra_line_y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">])</span>
                <span class="n">h_val_line_y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">h_tra_line_y</span><span class="p">,</span> <span class="n">lw</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;--&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">h_val_line_y</span><span class="p">,</span> <span class="n">lw</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;--&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span>
            <span class="n">patches</span><span class="o">.</span><span class="n">Rectangle</span><span class="p">(</span>
                <span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()[</span><span class="mi">0</span><span class="p">]),</span>  <span class="c1"># (x,y)</span>
                <span class="n">width</span><span class="o">=</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                <span class="n">height</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()[</span><span class="mi">1</span><span class="p">],</span>
                <span class="n">facecolor</span><span class="o">=</span><span class="s2">&quot;#000000&quot;</span><span class="p">,</span>
                <span class="n">alpha</span><span class="o">=</span><span class="mf">0.05</span>
            <span class="p">)</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_operator</span><span class="p">(</span><span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">)</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span><span class="p">:</span>
            <span class="n">legend_location</span> <span class="o">=</span> <span class="s1">&#39;lower right&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">legend_location</span> <span class="o">=</span> <span class="s1">&#39;upper right&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">legend</span><span class="p">([</span><span class="s1">&#39;Train&#39;</span><span class="p">,</span> <span class="s1">&#39;Validation&#39;</span><span class="p">],</span> <span class="n">loc</span><span class="o">=</span><span class="n">legend_location</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">([])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_1</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

        <span class="c1"># PLOT 2 / Highlighted area</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">cla</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;Metric / Highlighted area&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">][</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">][</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]],</span>
            <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">([])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">tick_right</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax2_2</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_label_position</span><span class="p">(</span><span class="s2">&quot;right&quot;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">mid</span><span class="p">,</span> <span class="n">metric_label</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">):</span>
            <span class="n">metric_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">metric_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_measure&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">metric_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;f_score&#39;</span><span class="p">):</span>
                <span class="n">factor</span> <span class="o">=</span> <span class="mi">100</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">factor</span> <span class="o">=</span> <span class="mi">1</span>

            <span class="c1"># PLOT 3 / Main</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">cla</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;External metric&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">metric_label</span><span class="p">))</span>

            <span class="n">mask</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
                <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">)[</span><span class="n">mask</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="n">mask</span><span class="p">]</span><span class="o">*</span><span class="n">factor</span><span class="p">,</span>
                <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
                <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span>
                <span class="n">marker</span><span class="o">=</span><span class="s1">&#39;o&#39;</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span>
                <span class="n">patches</span><span class="o">.</span><span class="n">Rectangle</span><span class="p">(</span>
                    <span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()[</span><span class="mi">0</span><span class="p">]),</span>  <span class="c1"># (x,y)</span>
                    <span class="n">width</span><span class="o">=</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                    <span class="n">height</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()[</span><span class="mi">1</span><span class="p">],</span>
                    <span class="n">facecolor</span><span class="o">=</span><span class="s2">&quot;#000000&quot;</span><span class="p">,</span>
                    <span class="n">alpha</span><span class="o">=</span><span class="mf">0.05</span>
                <span class="p">)</span>
            <span class="p">)</span>

            <span class="c1"># Horizontal lines</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">numpy</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="n">mask</span><span class="p">])):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_operator</span><span class="p">(</span><span class="n">metric</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">metric_label</span><span class="p">))</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span><span class="p">:</span>
                    <span class="n">h_extra_line_y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nanmax</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="n">mask</span><span class="p">]</span><span class="o">*</span><span class="n">factor</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">h_extra_line_y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nanmin</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="n">mask</span><span class="p">]</span><span class="o">*</span><span class="n">factor</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">h_extra_line_y</span><span class="p">,</span> <span class="n">lw</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;blue&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;--&#39;</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_operator</span><span class="p">(</span><span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">)</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span><span class="p">:</span>
                <span class="n">legend_location</span> <span class="o">=</span> <span class="s1">&#39;lower right&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">legend_location</span> <span class="o">=</span> <span class="s1">&#39;upper right&#39;</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">legend</span><span class="p">([</span><span class="s1">&#39;Validation&#39;</span><span class="p">],</span> <span class="n">loc</span><span class="o">=</span><span class="n">legend_location</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">([])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Epochs&#39;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">extra_main</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

            <span class="c1"># PLOT 3 / Highlighted area</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">cla</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;External metric / Highlighted area&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">metric_label</span><span class="p">))</span>
            <span class="n">highlight_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span><span class="o">*</span><span class="n">factor</span>
            <span class="n">mask</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="n">highlight_data</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
                <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">span</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">span</span><span class="p">[</span><span class="mi">1</span><span class="p">])[</span><span class="n">mask</span><span class="p">],</span>
                <span class="n">highlight_data</span><span class="p">[</span><span class="n">mask</span><span class="p">],</span>
                <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
                <span class="n">color</span><span class="o">=</span><span class="s1">&#39;green&#39;</span><span class="p">,</span>
                <span class="n">marker</span><span class="o">=</span><span class="s1">&#39;o&#39;</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">([])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Epochs&#39;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">tick_right</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_label_position</span><span class="p">(</span><span class="s2">&quot;right&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">extra_highlight</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">subplots_adjust</span><span class="p">(</span><span class="n">left</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="mf">0.95</span><span class="p">,</span> <span class="n">top</span><span class="o">=</span><span class="mf">0.9</span><span class="p">,</span> <span class="n">bottom</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">wspace</span><span class="o">=</span><span class="mf">0.02</span><span class="p">,</span> <span class="n">hspace</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_train_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;epochs&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">on_epoch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seen</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span>

        <span class="n">logs</span> <span class="o">=</span> <span class="n">logs</span> <span class="ow">or</span> <span class="p">{}</span>

        <span class="c1"># Collect values</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;metrics&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">logs</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log_values</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>
                <span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;loss&#39;</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_tra&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>

                <span class="k">elif</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;val_loss&#39;</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;l_val&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>

                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric</span> <span class="ow">and</span> <span class="n">k</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;val_&#39;</span><span class="p">):</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_val&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;m_tra&#39;</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">manual_update</span><span class="p">:</span>
            <span class="c1"># Update logged progress</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">timer</span><span class="o">.</span><span class="n">elapsed</span><span class="p">()</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">plotting_rate</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">flush_events</span><span class="p">()</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">interactive</span><span class="p">:</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">pause</span><span class="p">(</span><span class="mf">0.01</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">save</span><span class="p">:</span>
                <span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">bbox_inches</span><span class="o">=</span><span class="s1">&#39;tight&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">format</span><span class="p">,</span> <span class="n">dpi</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">add_external_metric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add external metric to be monitored</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        metric_label : str</span>
<span class="sd">            Metric label</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">metric_label</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">external_metric_labels</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_label</span>

        <span class="k">if</span> <span class="n">metric_label</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

    <span class="k">def</span> <span class="nf">set_external_metric_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">,</span> <span class="n">metric_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add external metric value</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        metric_label : str</span>
<span class="sd">            Metric label</span>

<span class="sd">        metric_value : numeric</span>
<span class="sd">            Metric value</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">external_metric</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_value</span>

    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Manually close progress logging</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">save</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">bbox_inches</span><span class="o">=</span><span class="s1">&#39;tight&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">format</span><span class="p">,</span> <span class="n">dpi</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">figure</span><span class="p">)</span></div>


<div class="viewcode-block" id="StopperCallback"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.StopperCallback.html#dcase_framework.keras_utils.StopperCallback">[docs]</a><span class="k">class</span> <span class="nc">StopperCallback</span><span class="p">(</span><span class="n">BaseCallback</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Keras callback to stop training when improvement has not seen in specified amount of epochs.</span>
<span class="sd">    Implements Keras Callback API.</span>

<span class="sd">    Callback is very similar to standard ``EarlyStopping`` Keras callback, however it adds support for external metrics</span>
<span class="sd">    (calculated outside Keras training process).</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="StopperCallback.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.StopperCallback.html#dcase_framework.keras_utils.StopperCallback.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        epochs : int</span>
<span class="sd">            Total amount of epochs</span>

<span class="sd">        manual_update : bool</span>
<span class="sd">            Manually update callback, use this to when injecting external metrics</span>
<span class="sd">            Default value True</span>

<span class="sd">        monitor : str</span>
<span class="sd">            Metric value to be monitored</span>
<span class="sd">            Default value &quot;val_loss&quot;</span>

<span class="sd">        patience : int</span>
<span class="sd">            Number of epochs with no improvement after which training will be stopped.</span>
<span class="sd">            Default value 0</span>

<span class="sd">        min_delta : float</span>
<span class="sd">            Minimum change in the monitored quantity to qualify as an improvement.</span>
<span class="sd">            Default value 0</span>

<span class="sd">        initial_delay : int</span>
<span class="sd">            Amount of epochs to wait at the beginning before quantity is monitored.</span>
<span class="sd">            Default value 10</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">StopperCallback</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;monitor&#39;</span><span class="p">,</span> <span class="s1">&#39;val_loss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">patience</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;patience&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_delta</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;min_delta&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_delay</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;initial_delay&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">wait</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stopped_epoch</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">params</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stopped</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span> <span class="o">=</span> <span class="p">{</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

        <span class="n">mode</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mode&#39;</span><span class="p">,</span> <span class="s1">&#39;auto&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;min&#39;</span><span class="p">,</span> <span class="s1">&#39;max&#39;</span><span class="p">,</span> <span class="s1">&#39;auto&#39;</span><span class="p">]:</span>
            <span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;auto&#39;</span>

        <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;min&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">less</span>
        <span class="k">elif</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;max&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_operator</span><span class="p">(</span><span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">best</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">Inf</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">less</span> <span class="k">else</span> <span class="o">-</span><span class="n">numpy</span><span class="o">.</span><span class="n">Inf</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_delta</span> <span class="o">*=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_delta</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span></div>

    <span class="k">def</span> <span class="nf">on_train_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">epochs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;epochs&#39;</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">wait</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wait</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">stopped_epoch</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stopped_epoch</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">on_epoch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span> <span class="ow">in</span> <span class="n">logs</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">manual_update</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">set_external_metric_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">,</span> <span class="n">metric_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add external metric value</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        metric_label : str</span>
<span class="sd">            Metric label</span>

<span class="sd">        metric_value : numeric</span>
<span class="sd">            Metric value</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">metric_label</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_value</span>

    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stopped</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_delay</span><span class="p">:</span>
            <span class="c1"># get current metric value</span>
            <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">current</span><span class="p">):</span>
                <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Metric to monitor is Nan, metric:[</span><span class="si">{metric}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                    <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span>
                <span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span><span class="p">(</span><span class="n">current</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">best</span><span class="p">):</span>
                <span class="c1"># New best value found</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">best</span> <span class="o">=</span> <span class="n">current</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">wait</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">wait</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">patience</span><span class="p">:</span>
                    <span class="c1"># Stopping criteria met =&gt; return false</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">stopped_epoch</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">stop_training</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;  Stopping criteria met at epoch[</span><span class="si">{epoch:d}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">epoch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span>
                    <span class="p">))</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;    metric[</span><span class="si">{metric}</span><span class="s1">], patience[</span><span class="si">{patience:d}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">,</span>
                        <span class="n">current</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:4.4f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">current</span><span class="p">),</span>
                        <span class="n">patience</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">patience</span>
                    <span class="p">))</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;  &#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">stopped</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stopped</span>

                <span class="c1"># Increase waiting counter</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">wait</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">last_update_epoch</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stopped</span></div>


<div class="viewcode-block" id="StasherCallback"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.StasherCallback.html#dcase_framework.keras_utils.StasherCallback">[docs]</a><span class="k">class</span> <span class="nc">StasherCallback</span><span class="p">(</span><span class="n">BaseCallback</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Keras callback to monitor training process and store best model. Implements Keras Callback API.</span>

<span class="sd">    This callback is very similar to standard ``ModelCheckpoint`` Keras callback, however it adds support for external</span>
<span class="sd">    metrics (metrics calculated outside Keras training process).</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="StasherCallback.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.StasherCallback.html#dcase_framework.keras_utils.StasherCallback.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        epochs : int</span>
<span class="sd">            Total amount of epochs</span>

<span class="sd">        manual_update : bool</span>
<span class="sd">            Manually update callback, use this to when injecting external metrics</span>
<span class="sd">            Default value True</span>

<span class="sd">        monitor : str</span>
<span class="sd">            Metric to monitor</span>
<span class="sd">            Default value &#39;val_loss&#39;</span>

<span class="sd">        mode : str</span>
<span class="sd">            Which way metric is interpreted, values {auto, min, max}</span>
<span class="sd">            Default value &#39;auto&#39;</span>

<span class="sd">        period : int</span>
<span class="sd">            Disable tqdm based progress bar</span>
<span class="sd">            Default value 1</span>

<span class="sd">        initial_delay : int</span>
<span class="sd">            Amount of epochs to wait at the beginning before quantity is monitored.</span>
<span class="sd">            Default value 10</span>

<span class="sd">        save_weights : bool</span>
<span class="sd">            Save weight to the disk</span>
<span class="sd">            Default value False</span>

<span class="sd">        file_path : str</span>
<span class="sd">            File name for model weight</span>
<span class="sd">            Default value None</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">StasherCallback</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;monitor&#39;</span><span class="p">,</span> <span class="s1">&#39;val_loss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">period</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;period&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_delay</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;initial_delay&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">save_weights</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;save_weights&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_path</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;file_path&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">epochs_since_last_save</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>

        <span class="n">mode</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mode&#39;</span><span class="p">,</span> <span class="s1">&#39;auto&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;auto&#39;</span><span class="p">,</span> <span class="s1">&#39;min&#39;</span><span class="p">,</span> <span class="s1">&#39;max&#39;</span><span class="p">]:</span>
            <span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;auto&#39;</span>

        <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;min&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">less</span>

        <span class="k">elif</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;max&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">greater</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_operator</span><span class="p">(</span><span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">)</span>


        <span class="bp">self</span><span class="o">.</span><span class="n">best</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">Inf</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span> <span class="o">==</span> <span class="n">numpy</span><span class="o">.</span><span class="n">less</span> <span class="k">else</span> <span class="o">-</span><span class="n">numpy</span><span class="o">.</span><span class="n">Inf</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span> <span class="o">=</span> <span class="p">{</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">best_model_weights</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">best_model_epoch</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">last_logs</span> <span class="o">=</span> <span class="kc">None</span></div>

    <span class="k">def</span> <span class="nf">on_epoch_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">=</span> <span class="n">epoch</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span> <span class="ow">in</span> <span class="n">logs</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">last_logs</span> <span class="o">=</span> <span class="n">logs</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">manual_update</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_delay</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">epochs_since_last_save</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">epochs_since_last_save</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">period</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">epochs_since_last_save</span> <span class="o">=</span> <span class="mi">0</span>

                <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">current</span><span class="p">):</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Metric to monitor is Nan, metric:[</span><span class="si">{metric}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                        <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span>
                    <span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor_op</span><span class="p">(</span><span class="n">current</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">best</span><span class="p">):</span>

                        <span class="c1"># Store the best</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">best</span> <span class="o">=</span> <span class="n">current</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">best_model_weights</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_weights</span><span class="p">()</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">best_model_epoch</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">epoch</span>

                        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_weights</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="p">:</span>
                            <span class="c1"># Save weight on disk</span>
                            <span class="n">logs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_logs</span>
                            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitor</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">logs</span><span class="p">:</span>
                                <span class="n">logs</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">]</span> <span class="o">=</span> <span class="n">current</span>

                            <span class="n">file_path</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">epoch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">last_logs</span><span class="p">)</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save_weights</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_external_metric_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric_label</span><span class="p">,</span> <span class="n">metric_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add external metric value</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        metric_label : str</span>
<span class="sd">            Metric label</span>

<span class="sd">        metric_value : numeric</span>
<span class="sd">            Metric value</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">metric_label</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">epochs</span><span class="p">,))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][:]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">nan</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metric_data</span><span class="p">[</span><span class="n">metric_label</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">epoch</span><span class="p">]</span> <span class="o">=</span> <span class="n">metric_value</span>

    <span class="k">def</span> <span class="nf">get_best</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return best model seen</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        dict</span>
<span class="sd">            Dictionary with keys &#39;weights&#39;, &#39;epoch&#39;, &#39;metric_value&#39;</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;epoch&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_model_epoch</span><span class="p">,</span>
            <span class="s1">&#39;weights&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_model_weights</span><span class="p">,</span>
            <span class="s1">&#39;metric_value&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">best</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">log</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print information about the best model into logging interface</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;  Best model weights at epoch[</span><span class="si">{epoch:d}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">epoch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">best_model_epoch</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;    metric[</span><span class="si">{metric}</span><span class="s1">]=</span><span class="si">{best}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">monitor</span><span class="p">,</span>
                <span class="n">best</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">{:4.4f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">best</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;  &#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="BaseDataGenerator"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.html#dcase_framework.keras_utils.BaseDataGenerator">[docs]</a><span class="k">class</span> <span class="nc">BaseDataGenerator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for data generator.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="BaseDataGenerator.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.html#dcase_framework.keras_utils.BaseDataGenerator.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        files : list</span>

<span class="sd">        data_filenames : dict</span>

<span class="sd">        annotations : dict</span>

<span class="sd">        class_labels : list of str</span>

<span class="sd">        hop_length_seconds : float</span>
<span class="sd">            Default value 0.2</span>

<span class="sd">        shuffle : bool</span>
<span class="sd">            Default value True</span>

<span class="sd">        batch_size : int</span>
<span class="sd">            Default value 64</span>

<span class="sd">        buffer_size : int</span>
<span class="sd">            Default value 256</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="s1">&#39;base_generator&#39;</span>

        <span class="c1"># Data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">item_list</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;files&#39;</span><span class="p">,</span> <span class="p">[]))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_filenames</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;data_filenames&#39;</span><span class="p">,</span> <span class="p">{})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">annotations</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;annotations&#39;</span><span class="p">,</span> <span class="p">{})</span>

        <span class="c1"># Activity matrix</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;class_labels&#39;</span><span class="p">,</span> <span class="p">[])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hop_length_seconds</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;hop_length_seconds&#39;</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">shuffle</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;shuffle&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;batch_size&#39;</span><span class="p">,</span> <span class="mi">64</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffer_size</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;buffer_size&#39;</span><span class="p">,</span> <span class="mi">256</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;logger&#39;</span><span class="p">,</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">))</span>

        <span class="c1"># Internal state variables</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">batch_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">item_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_position</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="c1"># Initialize data buffer</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span> <span class="o">=</span> <span class="n">DataBuffer</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">buffer_size</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffer_size</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">item_list</span><span class="p">):</span>
            <span class="c1"># Fill data buffer at initialization if it fits fully to the buffer</span>

            <span class="k">for</span> <span class="n">current_item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">item_list</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">process_item</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="n">current_item</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">full</span><span class="p">():</span>
                    <span class="k">break</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_data_size</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_input_size</span> <span class="o">=</span> <span class="kc">None</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">steps_count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Number of batches in one epoch</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">num_batches</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_size</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">)))</span>

        <span class="k">if</span> <span class="n">num_batches</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">num_batches</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">input_size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Length of input feature vector</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_input_size</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Load first item</span>
            <span class="n">first_item</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_filenames</span><span class="o">.</span><span class="n">keys</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">process_item</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="n">first_item</span><span class="p">)</span>

            <span class="c1"># Get Feature vector length</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_input_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">first_item</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_input_size</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">data_size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Total data amount</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data_size</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_data_size</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">for</span> <span class="n">current_item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">item_list</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">process_item</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="n">current_item</span><span class="p">)</span>
                <span class="n">data</span><span class="p">,</span> <span class="n">meta</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">current_item</span><span class="p">)</span>

                <span class="c1"># Accumulate feature matrix length</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_data_size</span> <span class="o">+=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data_size</span>

<div class="viewcode-block" id="BaseDataGenerator.info"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.BaseDataGenerator.info.html#dcase_framework.keras_utils.BaseDataGenerator.info">[docs]</a>    <span class="k">def</span> <span class="nf">info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Information logging</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">info</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s1">&#39;  Generator&#39;</span><span class="p">,</span>
            <span class="s1">&#39;    Shuffle </span><span class="se">\t</span><span class="s1">[</span><span class="si">{shuffle}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">shuffle</span><span class="o">=</span><span class="s1">&#39;True&#39;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">shuffle</span> <span class="k">else</span> <span class="s1">&#39;False&#39;</span><span class="p">),</span>
            <span class="s1">&#39;    Epoch size</span><span class="se">\t</span><span class="s1">[</span><span class="si">{steps:d}</span><span class="s1"> batches]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">steps</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">steps_count</span><span class="p">),</span>
            <span class="s1">&#39;    Buffer size </span><span class="se">\t</span><span class="s1">[</span><span class="si">{buffer_size:d}</span><span class="s1"> files]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">buffer_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">buffer_size</span><span class="p">),</span>
            <span class="s1">&#39; &#39;</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">info</span></div>

    <span class="k">def</span> <span class="nf">process_item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_epoch_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span></div>


<div class="viewcode-block" id="FeatureGenerator"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.FeatureGenerator.html#dcase_framework.keras_utils.FeatureGenerator">[docs]</a><span class="k">class</span> <span class="nc">FeatureGenerator</span><span class="p">(</span><span class="n">BaseDataGenerator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Feature data generator</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="FeatureGenerator.__init__"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.FeatureGenerator.html#dcase_framework.keras_utils.FeatureGenerator.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Constructor</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        files : list of str</span>
<span class="sd">            List of active item identifies, usually filenames</span>

<span class="sd">        data_filenames : dict of dicts</span>
<span class="sd">            Data structure keyed with item identifiers (defined with files parameter), data dict feature extractor</span>
<span class="sd">            labels as keys and values the filename on disk.</span>

<span class="sd">        annotations : dict of MetaDataContainers or MetaDataItems</span>
<span class="sd">            Annotations for all items keyed with item identifiers</span>

<span class="sd">        class_labels : list of str</span>
<span class="sd">            Class labels in a list</span>

<span class="sd">        hop_length_seconds : float</span>
<span class="sd">            Analysis frame hop length in seconds</span>
<span class="sd">            Default value 0.2</span>

<span class="sd">        shuffle : bool</span>
<span class="sd">            Shuffle data before each epoch</span>
<span class="sd">            Default value True</span>

<span class="sd">        batch_size : int</span>
<span class="sd">            Batch size to generate</span>
<span class="sd">            Default value 64</span>

<span class="sd">        buffer_size : int</span>
<span class="sd">            Internal item buffer size, set large enough for smaller dataset to avoid loading</span>
<span class="sd">            Default value 256</span>

<span class="sd">        data_processor : class</span>
<span class="sd">            Data processor class used to process load features</span>

<span class="sd">        data_refresh_on_each_epoch : bool</span>
<span class="sd">            Internal data buffer reset at the start of each epoch</span>
<span class="sd">            Default value False</span>

<span class="sd">        label_mode : str (&#39;event&#39;, &#39;scene&#39;)</span>
<span class="sd">            Activity matrix forming mode.</span>
<span class="sd">            Default value &quot;event&quot;</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">data_processor</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;data_processor&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_refresh_on_each_epoch</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;data_refresh_on_each_epoch&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">label_mode</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;label_mode&#39;</span><span class="p">,</span> <span class="s1">&#39;event&#39;</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">FeatureGenerator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="s1">&#39;feature&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;logger&#39;</span><span class="p">,</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">label_mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">,</span> <span class="s1">&#39;scene&#39;</span><span class="p">]:</span>
            <span class="n">message</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{name}</span><span class="s1">: Label mode unknown [</span><span class="si">{label_mode}</span><span class="s1">]&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">metric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">label_mode</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">process_item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">key_exists</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">item</span><span class="p">):</span>
            <span class="n">current_data</span><span class="p">,</span> <span class="n">current_length</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_processor</span><span class="o">.</span><span class="n">load</span><span class="p">(</span>
                <span class="n">feature_filename_dict</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">data_filenames</span><span class="p">[</span><span class="n">item</span><span class="p">]</span>
            <span class="p">)</span>
            <span class="n">current_activity_matrix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_activity_matrix</span><span class="p">(</span>
                <span class="n">annotation</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">annotations</span><span class="p">[</span><span class="n">item</span><span class="p">],</span>
                <span class="n">data_length</span><span class="o">=</span><span class="n">current_length</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">item</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">current_data</span><span class="p">,</span> <span class="n">meta</span><span class="o">=</span><span class="n">current_activity_matrix</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">on_epoch_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">batch_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">shuffle</span><span class="p">:</span>
            <span class="c1"># Shuffle item list order</span>
            <span class="n">numpy</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">item_list</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_refresh_on_each_epoch</span><span class="p">:</span>
            <span class="c1"># Force reload of data</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>

<div class="viewcode-block" id="FeatureGenerator.generator"><a class="viewcode-back" href="../../generated/dcase_framework.keras_utils.FeatureGenerator.generator.html#dcase_framework.keras_utils.FeatureGenerator.generator">[docs]</a>    <span class="k">def</span> <span class="nf">generator</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generator method</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ndarray</span>
<span class="sd">            data batches</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="c1"># Start of epoch</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">on_epoch_start</span><span class="p">()</span>

            <span class="n">batch_buffer_data</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">batch_buffer_meta</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="c1"># Go through items</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">item_list</span><span class="p">:</span>
                <span class="c1"># Load item data into buffer</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">process_item</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="n">item</span><span class="p">)</span>

                <span class="c1"># Fetch item from buffer</span>
                <span class="n">data</span><span class="p">,</span> <span class="n">meta</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_buffer</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">item</span><span class="p">)</span>

                <span class="c1"># Data indexing</span>
                <span class="n">data_ids</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

                <span class="c1"># Shuffle data order</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">shuffle</span><span class="p">:</span>
                    <span class="n">numpy</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">data_ids</span><span class="p">)</span>

                <span class="k">for</span> <span class="n">data_id</span> <span class="ow">in</span> <span class="n">data_ids</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">batch_buffer_data</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">:</span>
                        <span class="c1"># Batch buffer full, yield data</span>
                        <span class="k">yield</span> <span class="p">(</span>
                            <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span>
                                <span class="n">numpy</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">batch_buffer_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                            <span class="p">),</span>
                            <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span>
                                <span class="n">numpy</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">batch_buffer_meta</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                            <span class="p">)</span>
                        <span class="p">)</span>

                        <span class="c1"># Empty batch buffers</span>
                        <span class="n">batch_buffer_data</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="n">batch_buffer_meta</span> <span class="o">=</span> <span class="p">[]</span>

                        <span class="c1"># Increase batch counter</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">batch_index</span> <span class="o">+=</span> <span class="mi">1</span>

                    <span class="c1"># Collect data fro the batch</span>
                    <span class="n">batch_buffer_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">data_id</span><span class="p">])</span>
                    <span class="n">batch_buffer_meta</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">meta</span><span class="p">[</span><span class="n">data_id</span><span class="p">])</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">batch_buffer_data</span><span class="p">):</span>
                <span class="c1"># Last batch, usually not full</span>
                <span class="k">yield</span> <span class="p">(</span>
                    <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span>
                        <span class="n">numpy</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">batch_buffer_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                    <span class="p">),</span>
                    <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span>
                        <span class="n">numpy</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">batch_buffer_meta</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                    <span class="p">),</span>
                <span class="p">)</span>
                <span class="c1"># Increase batch counter</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">batch_index</span> <span class="o">+=</span> <span class="mi">1</span>

            <span class="c1"># End of epoch</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">on_epoch_end</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">get_activity_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">annotation</span><span class="p">,</span> <span class="n">data_length</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Convert annotation into activity matrix and run it through data processor.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">event_roll</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">label_mode</span> <span class="o">==</span> <span class="s1">&#39;event&#39;</span><span class="p">:</span>
            <span class="c1"># Event activity, event onset and offset specified</span>
            <span class="n">event_roll</span> <span class="o">=</span> <span class="n">EventRoll</span><span class="p">(</span><span class="n">metadata_container</span><span class="o">=</span><span class="n">annotation</span><span class="p">,</span>
                                   <span class="n">label_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="p">,</span>
                                   <span class="n">time_resolution</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">hop_length_seconds</span>
                                   <span class="p">)</span>
            <span class="n">event_roll</span> <span class="o">=</span> <span class="n">event_roll</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">length</span><span class="o">=</span><span class="n">data_length</span><span class="p">)</span>

        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">label_mode</span> <span class="o">==</span> <span class="s1">&#39;scene&#39;</span><span class="p">:</span>
            <span class="c1"># Scene activity, one-hot activity throughout whole file</span>
            <span class="n">pos</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">annotation</span><span class="o">.</span><span class="n">scene_label</span><span class="p">)</span>
            <span class="n">event_roll</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">data_length</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">class_labels</span><span class="p">)))</span>
            <span class="n">event_roll</span><span class="p">[:,</span> <span class="n">pos</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="k">if</span> <span class="n">event_roll</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_processor</span><span class="o">.</span><span class="n">process_activity_data</span><span class="p">(</span>
                <span class="n">activity_data</span><span class="o">=</span><span class="n">event_roll</span>
            <span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div>
</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2017, Toni Heittola et al..

    </p>
  </div> 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>